Qt Creator 4.0.2: Move window defined in main.cpp from slot defined in mainwindow.cpp
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
Code:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *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?
Re: Qt Creator 4.0.2: Move window defined in main.cpp from slot defined in mainwindow
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().
Re: Qt Creator 4.0.2: Move window defined in main.cpp from slot defined in mainwindow
Oh, that's just too "cute".
Thanks a bunch!