QDialog::exec react after 3 calls
-
I'm using a custom dialog, and I'm showing it by calling:
@ this->numberPick.move(point);
this->numberPick.setWindowFlags(Qt::SplashScreen);
this->numberPick.setParent(this);
this->numberPick.setModal(true);
this->numberPick.exec();@All this code is invoked in void MainWindow::on_boardView_clicked(const QModelIndex &index){} (event "clicked" from QTableView).
The problem is quite strange. I need to click on my button for 3 times for dialog to show. After second click Qt Creator gives me warning like this "QDialog::exec: Recursive call detected".
Funny thing is that this happens only once (in single application instance). Next clicks works like a charm.
-
where is the code you posted placed? In which method?
-
void MainWindow::on_boardView_clicked(const QModelIndex &index){}
Its method automatically created by Qt Creator for QTableView slot "clicked"
-
ok looks good so far ... how does your subclass of QDialog look like?
-
cpp:
@#include "numberpick.h"
#include "ui_numberpick.h"NumberPick::NumberPick(QWidget *parent) :
QDialog(parent),
ui(new Ui::NumberPick)
{
ui->setupUi(this);
}NumberPick::~NumberPick()
{
delete ui;
}void NumberPick::setCell(Cell *cell)
{
this->cell = cell;
}void NumberPick::on_pushButton_11_clicked()
{
//przycisk "zatwierdź"
this->hide();
}void NumberPick::on_pushButton_10_clicked()
{
//przycisk "wyzeruj pole"
this->cell->setVal(0);
this->hide();
}@h:
@#ifndef NUMBERPICK_H
#define NUMBERPICK_H#include <QDialog>
#include "Cell.h"namespace Ui {
class NumberPick;
}class NumberPick : public QDialog
{
Q_OBJECTpublic:
explicit NumberPick(QWidget *parent = 0);
~NumberPick();
void setCell(Cell * cell);private slots:
void on_pushButton_11_clicked();void on_pushButton_10_clicked();
private:
Ui::NumberPick *ui;
Cell * cell;
};#endif // NUMBERPICK_H
@ -
thats strange... do you connect to the slot multiple times maybe?
-
Actually i don't connect it myself. QtCreator did all the job, i just right-clicked TableView and clicked "go to slot".
Though there is a little fact about my TableView. I'm painting all its cells with delegate. Could that be important? -
no that's not the problem.
You problem is that QDialog::exec() gets called multiple times. But your dialog implementation is very simple and i don't see any code where this should happen. There must be some place where the exec() is called again... -
Nope, no other place.. whole code of my event:
@void MainWindow::on_boardView_clicked(const QModelIndex &index)
{
QPoint point = QCursor::pos();
point.setX(point.x() - this->numberPick.width() / 2);
point.setY(point.y() - this->numberPick.height() / 2);Cell * cell = this->delegate->getSudoku()->getCell(index.row()+1, index.column()+1); if(cell->getClicked() && !cell->getFixed()){ //TODO : naprawić wyskakujące okienko this->numberPick.setCell(cell); this->numberPick.move(point); this->numberPick.setWindowFlags(Qt::SplashScreen); this->numberPick.setParent(this); this->numberPick.setModal(true); this->numberPick.exec(); this->ui->boardView->viewport()->update(); }else{ this->delegate->getSudoku()->clickCell(index.row()+1, index.column()+1); this->ui->boardView->viewport()->update(); } cell->setClicked(true);
}@
There is no other place that dialog could be used
-
Ok i did little workaround and here's my result:
@this->numberPick = new NumberPick(this);
this->numberPick->setCell(cell);
this->numberPick->move(point);
this->numberPick->setWindowFlags(Qt::SplashScreen);
this->numberPick->exec();
delete this->numberPick;@It works, but every time i open this dialog i get this message like 20 times "nativeResourceForWindow: 'handle' requested for null window or window without handle.". Should i fix that or no need to be worried (couse whole app works great now)?
-
now i got whats wrong with your code from the first post ;)
Your hiding of the dialog is wrong.
Since you start your dialog with QDialog::exec() a new event loop is started. In this case you shouldn't "close" the dialog with a simple hide() call, because this just hides the dialog but the started event loop remains and isn't left. There you have your multiple call of exec() i was talking before.
You should use QDialog::done() instead which closes the dialog and returns from the event loop.
-
and which code would be better? Creating a new window every time could have impact on application performance, right?
-
you won't notice a significant impact on performance.
The best would be to create it on the heap (using "new") and reuse it. Thus it is only created if it is needed once.
Initialize the pointer to "0" and only create the dialog if the pointer is "0". Otherwise just call exec() on it:
@
if( ! numberPick )
{
//create and initialize dialog
}
numberPick.exec();
@ -
Ok, all works just fine. Thx for your help!