on Dialog box show() is not working but exec() is
-
@Pl45m4
Here is what I have right now, allthough update() is never called and I have no idea why
void Object::start(){
ui = new Object2 (nullptr);
ui->open();
QTimer *timer = new QTimer(this);QObject::connect(timer,SIGNAL(timeout()),this,SLOT(update())); timer->start(1000);
}
void Object::update(){
qInfo("Hi");
} -
@rupertrupert said in on Dialog box show() is not working but exec() is:
update() is never called and I have no idea why
Because your signal calls
QWidget::update()
and not yourObject::update()
function.Rename your function to something like
sayHi()
and it should work like expectedBtw: you are leaking memory cause you create a new instance of
Object2
every timeObject::start()
is called. And since your dialog has no valid parent it's not using the auto-cleanup from Qt MetaObject system.Add
Qt::WA_DeleteOnClose
as attribute to your dialog or create in just once in your c'tor, then only open/show it when you need it. -
@Pl45m4 said in on Dialog box show() is not working but exec() is:
Because your signal calls
QWidget::update()
and not yourObject::update()
function.Although this would explain behaviour, OOI what makes you say
QWidget::update()
will be called? The slot is onthis
, which is aObject
with its ownupdate()
, and what makes you thinkObject
is aQWidget
?Doubtless @Pl45m4 will turn out to be right though I'm not sure why/how he knows. Otherwise what is the scope/lifetime of the
Object
instance thatstart()
is called on? Or, again, afterstart()
do you allow the event loop to be re-entered? -
@JonB said in on Dialog box show() is not working but exec() is:
OOI what makes you say QWidget::update() will be called?
The described behavior by OP
The slot is on this, which is a Object with its own update()
True, but OP uses the String-based connections which are not fully qualified and don't specify a type or class.
From my own experience I'm 99.99% sure ;-)what makes you think Object is a QWidget?
Must be, otherwise it would have worked ;-)
[Edit: Apparently it's actually aQObject
but some other weird stuff is going on. Maybe app quit?!]Btw:
Using a fully qualified functor will also solve this:// Object::update(), correct QObject::connect(timer, &QTimer::timeout, this, &Object::update); // QWidget::update(), wrong QObject::connect(timer, SIGNAL(timeout()), this, SLOT(update()));
&Object::update
will explicitely pickvoid Object::update()
by function reference, which prints "Hi" in this case.
While the String/Macro based syntax just sends aupdate()
call tothis
without any checks at runtime, which results inQWidget::update()
being called. -
@rupertrupert said in on Dialog box show() is not working but exec() is:
OK i tried both of the solutions you said and they did not work
If the below code does not work, you are still blocking the signal with some
sleep
orwhile
- loop.
You must not have anysleep
or blocking loops in your main/GUI thread in your entire program... otherwise it breaks event driven mechanics as used in Qt.void Object::start() { ui = new Object2(this); ui->open(); QTimer *timer = new QTimer(this); QObject::connect(timer, &QTimer::timeout, this, &Object::update); timer->start(1000); }
-
@Pl45m4 said in on Dialog box show() is not working but exec() is:
QObject::connect(timer, &QTimer::timeout, this, &Object::update);
ok I changed to exactly this and now i dont see my ui or anything and the program just closes when I reach Object::start(), and there is no code after Object::start() is called to be blocking the signal
-
@rupertrupert said in on Dialog box show() is not working but exec() is:
there is no code after Object::start()
Where do you call
start()
? InMainWindow
? Or in yourmain.cpp
?How is your
Object
created and what kind of class is this? As @JonB was also wondering... -
@rupertrupert said in on Dialog box show() is not working but exec() is:
after my main window is closed
Usually your program would end at this point...
Post the related code then... this isn't a Q&A game here.
We can't see what your are doing exactly