Results 1 to 3 of 3

Thread: Qt Creator 4.0.2: Move window defined in main.cpp from slot defined in mainwindow.cpp

  1. #1
    Join Date
    Apr 2016
    Posts
    23
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default 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
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. MainWindow::~MainWindow()
    12. {
    13. delete ui;
    14. }
    15.  
    16. void MainWindow::on_pushButton_clicked()
    17. {
    18. w.move( 100, 100 );
    19. }
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default 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().
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  3. #3
    Join Date
    Apr 2016
    Posts
    23
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default 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!

Similar Threads

  1. Replies: 6
    Last Post: 14th January 2013, 23:43
  2. Replies: 6
    Last Post: 3rd December 2012, 08:26
  3. window sizes changing even though max size defined
    By JonathanForQT4 in forum Newbie
    Replies: 3
    Last Post: 6th August 2007, 11:39
  4. Replies: 4
    Last Post: 26th June 2007, 20:19
  5. How to capture exit slot of main window
    By jyoti kumar in forum Newbie
    Replies: 2
    Last Post: 31st May 2006, 08:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.