PDA

View Full Version : Qt Creator 4.0.2: Move window defined in main.cpp from slot defined in mainwindow.cpp



mdavidjohnson
8th November 2016, 00:58
I've searched the docs and the forums but can't figure out how to do this.

I open a new project in Qt Creator 4.0.2 and add one pushbutton to the form. When I click the pushbutton, I want the entire form to move to ( 100, 100 ).

main.cpp


#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}


mainwindow.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButton_clicked()
{
w.move( 100, 100 );
}


The last line, "w.move( 100, 100 ); doesn't work, of course, because w is defined in main.cpp, not mainwindow.cpp.

Substituting "ui" for "w" also doesn't work because there is no "move" function for ui.

"ui->centralWidget->move( 100, 100 );" works but it only moves the central widget, not the window.

What am I missing?

jefftee
8th November 2016, 01:13
The w variable is an instance of the MainWindow you allocated on the stack in main, so when you're inside of a method in your MainWindow class, just use move().

mdavidjohnson
8th November 2016, 04:44
Oh, that's just too "cute".

Thanks a bunch!