[solved] Java's runlater for C++
-
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??
-
what about overriding mainform sizeEvent ?
void MainWin::resizeEvent( QResizeEvent * event)
{
adjustSize();
}That way it will also work if user resize your window.
-
I already did that, but it does not work. No idea why. But i solved it by calling
this.show()
in the constructor beforecontroler->adjustSize();
-
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 connectionBut 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). -
This post is deleted!
-
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: ... };
-
@QT-static-prgm
hi
Controller must have
Q_OBJECT macro
When you use signals/slots
likeclass Controller : public QObject
{
Q_OBJECT
....Make sure to clean and really rebuild after adding.
-
I get some build errors now:
controller.o:-1: In functionZN10ControllerC2EP10MainWindow': controller.cpp:16: Fehler: undefined reference to
vtable for Controller'
controller.o:-1: In functionZN10ControllerD2Ev': controller.cpp:21: Fehler: undefined reference to
vtable for Controller'
collect2.exe:-1: Fehler: error: ld returned 1 exit statushere 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
-
Super :)
Good luck with your game