Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] Java's runlater for C++
Forum Update on Monday, May 27th 2025

[solved] Java's runlater for C++

Scheduled Pinned Locked Moved General and Desktop
c++runlaterpostconstructor
9 Posts 3 Posters 2.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    QT-static-prgm
    wrote on 31 Jul 2015, 17:04 last edited by QT-static-prgm 8 Jan 2015, 10:33
    #1

    Hi,

    i'm just wondering if there is a way in c++ to run an function AFTER an class constructor. The way platform.runlater is doing it for Java.

    The problem is, that i have some content that is displayed in the GraphicsView. An update function adjust the size of the GraphicsScene elements. All is working fine, but the initial size adjustment does not work. I started the debuger and found the problem.
    The adjustSize() function is called in the MainWindow constructor and uses the GraphicsView size to calculate the GraphicsScene element's size. But before the constructor isn't left the GraphicsView does not exist. So there is no real size for it and the calculation is wrong.

    Is there any way to call the adjustSize() function after the Mainwindow constructor??

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 31 Jul 2015, 17:12 last edited by
      #2

      what about overriding mainform sizeEvent ?

      void MainWin::resizeEvent( QResizeEvent * event)
      {
      adjustSize();
      }

      That way it will also work if user resize your window.

      http://doc.qt.io/qt-4.8/qresizeevent.html#details

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        QT-static-prgm
        wrote on 31 Jul 2015, 17:52 last edited by
        #3

        I already did that, but it does not work. No idea why. But i solved it by calling this.show() in the constructor before controler->adjustSize();

        1 Reply Last reply
        0
        • A Offline
          A Offline
          alex_malyu
          wrote on 31 Jul 2015, 23:24 last edited by alex_malyu
          #4

          Show in constructor sounds quite evil.

          The typical way to solve the problem is to use event loop.
          When using Qt you could constuct and post an event, but this can be replaced with emitting signal connected to the slot using queued connection

          But the simplest probably will be to use QTimer

          QTimer::singleShot(0, this, SLOT(doWHatYouNeed()));
          Above is very useful for widget initialization which has to be done after it is already shown (sizes are already determinated).

          1 Reply Last reply
          1
          • Q Offline
            Q Offline
            QT-static-prgm
            wrote on 1 Aug 2015, 08:56 last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • Q Offline
              Q Offline
              QT-static-prgm
              wrote on 1 Aug 2015, 09:25 last edited by
              #6

              I just tried it, but it does not work.
              I get this consol message: QMetaObject::invokeMethod: No such method QObject::adjustSize()

              That's what my Mainwindow Constructor is looking:

              MainWindow::MainWindow(QWidget *parent) :
              	QMainWindow(parent),
              	ui(new Ui::MainWindow)
              {
              	ui->setupUi(this);
              	scene = new QGraphicsScene;
              	controller = new Controller(this);
              
              	ui->graphicsView->setScene(scene);
              
              	ui->graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
              	ui->graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
              
              
              	//this->show();
              	controller->initField();
              	controller->initPlayer();
              	controller->drawField(scene);
              
              	QTimer::singleShot(0, controller, SLOT(adjustSize()));
              
              }
              

              and this is my Controller's class definition

              class Controller : public QObject
              {
              	public:
              		Controller(MainWindow* window);
              		~Controller();
              
              	private:
              		MainWindow* ui;
              		...
              
              	public slots:
              		void adjustSize();
              
              	public:
              		...
              
              };
              
              M 1 Reply Last reply 1 Aug 2015, 10:10
              0
              • Q QT-static-prgm
                1 Aug 2015, 09:25

                I just tried it, but it does not work.
                I get this consol message: QMetaObject::invokeMethod: No such method QObject::adjustSize()

                That's what my Mainwindow Constructor is looking:

                MainWindow::MainWindow(QWidget *parent) :
                	QMainWindow(parent),
                	ui(new Ui::MainWindow)
                {
                	ui->setupUi(this);
                	scene = new QGraphicsScene;
                	controller = new Controller(this);
                
                	ui->graphicsView->setScene(scene);
                
                	ui->graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
                	ui->graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
                
                
                	//this->show();
                	controller->initField();
                	controller->initPlayer();
                	controller->drawField(scene);
                
                	QTimer::singleShot(0, controller, SLOT(adjustSize()));
                
                }
                

                and this is my Controller's class definition

                class Controller : public QObject
                {
                	public:
                		Controller(MainWindow* window);
                		~Controller();
                
                	private:
                		MainWindow* ui;
                		...
                
                	public slots:
                		void adjustSize();
                
                	public:
                		...
                
                };
                
                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 1 Aug 2015, 10:10 last edited by mrjj 8 Jan 2015, 10:10
                #7

                @QT-static-prgm
                hi
                Controller must have
                Q_OBJECT macro
                When you use signals/slots
                like

                class Controller : public QObject
                {
                Q_OBJECT
                ....

                Make sure to clean and really rebuild after adding.

                1 Reply Last reply
                0
                • Q Offline
                  Q Offline
                  QT-static-prgm
                  wrote on 1 Aug 2015, 10:30 last edited by QT-static-prgm 8 Jan 2015, 10:33
                  #8

                  I get some build errors now:
                  controller.o:-1: In function ZN10ControllerC2EP10MainWindow': controller.cpp:16: Fehler: undefined reference to vtable for Controller'
                  controller.o:-1: In function ZN10ControllerD2Ev': controller.cpp:21: Fehler: undefined reference to vtable for Controller'
                  collect2.exe:-1: Fehler: error: ld returned 1 exit status

                  here are the lines 8 to 24

                  Controller::Controller(MainWindow *window):
                  	ui(window),
                  	startField(NULL),
                  	prison(NULL),
                  	currentPlayer(0),
                  	repeat(0),
                  	diceValue(0),
                  	anzHori(0),
                  	anzVerti(0)
                  {
                  
                  }
                  
                  Controller::~Controller()
                  {
                  
                  }
                  

                  ==EDIT==

                  OK cleaning the project deleted everthing excepted the controller.o file o.O After i manuel delete it and rebuild everything is now working. Thank you very much for your help

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 1 Aug 2015, 10:43 last edited by
                    #9

                    Super :)
                    Good luck with your game

                    1 Reply Last reply
                    0

                    1/9

                    31 Jul 2015, 17:04

                    • Login

                    • Login or register to search.
                    1 out of 9
                    • First post
                      1/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved