[SOLVED] refresh QMainWindow from another Dialog
-
Hi everybody,
I want to add an Item in a QComboBox which is in my topWidget from another QDialog. The topWidget is in the QMainWindow.
QMainWindow
@
#include "testcombobox.h"
#include "widtop.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
widtop *widt = new widtop();
ui->topLayout->addWidget(widt);
}MainWindow::~MainWindow()
{
delete ui;
}
@QWidget
@
#include "widtop.h"
#include "ui_widtop.h"widtop::widtop(QWidget *parent) :
QDialog(parent),
ui(new Ui::widtop)
{
ui->setupUi(this);
ui->comboBox->addItem("Item1");
ui->comboBox->addItem("Item2");
}widtop::~widtop()
{
delete ui;
}void widtop::addItemFunction()
{
ui->comboBox->addItem("You did it !");
}
@QDialog
@
testcombobox::testcombobox(QWidget *parent) :
QDialog(parent),
ui(new Ui::testcombobox)
{
ui->setupUi(this);
connect(ui->pushButton, SIGNAL(clicked(), .... , .... )
}testcombobox::~testcombobox()
{
delete ui;
}
@If this problem can be solved with a signal, I don't know what to write in it, but maybe there is another solution ?
Thank you in advance, Adrien.
-
Hum, that was what I mean but, it didn't solve my problem.
Like I said, "I want to add an Item in a QComboBox which is in my topWidget from another QDialog. The topWidget is in the QMainWindow.".
So with this connection, I can call the function to add an Item but it seems that the QMainWindow does not refresh or something like that.
If I put the same addItemFunction in the QMainWindow and trigger it with a QButton, it works but I don't want to push any button to refresh.
How to "refresh" my QMainWindow or forcing to reload the widget ?
-
You can use QApplication::processEvents() to force execution of all pending events in applications event loop, but I don't think that that's your problem. Try this:
@testcombobox::testcombobox(QWidget *parent, widtop *wt) :
QDialog(parent),
ui(new Ui::testcombobox)
{
ui->setupUi(this);
connect(ui->pushButton, SIGNAL(clicked(), wt , SLOT(addItemFunction()) )
}@where you would pass an pointer to instance of widtop that you want to be updated. If this is not working, try making your dialogs non modal (if they are).
-
I just tried with non modal window and QApplication::processEvents() and nothing happens.
The comboBox stay only with "Item1" and "Item2" previously added.
Maybe there is no way to modify a QMainWindow from another dialog ? In this case I will simply create a refresh button :/
-
Hi,
There's one thing that you don't show, it's where you do call your dialog and how you use it
-
Hello,
Okay, I will explain again very clearly. I have just created a new project to test and test again.
QMainWindow
@
#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
widtop *w = new widtop();
ui->layout->addWidget(w);
}MainWindow::~MainWindow()
{
delete ui;
}
@QWidget
@
#include "widtop.h"
#include "ui_widtop.h"
#include "dialog.h"widtop::widtop(QWidget *parent) :
QWidget(parent),
ui(new Ui::widtop)
{
ui->setupUi(this);
}widtop::~widtop()
{
delete ui;
}void widtop::addItemFunction()
{
ui->comboBox->addItem("You did it");
}void widtop::on_pushButton_clicked()
{
dialog *t = new dialog();
t->setModal(false);
t->exec();
}
@QDialog
@
#include "dialog.h"
#include "ui_dialog.h"
#include "widtop.h"dialog::dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::dialog)
{
ui->setupUi(this);
widtop *w = new widtop();
connect(ui->pushButton, SIGNAL(clicked()), w, SLOT(addItemFunction()));
//QApplication::processEvents();
}dialog::~dialog()
{
delete ui;
}
@And a little screenshot: http://upz.fr/c7Ta6.png
If you have some ideas to move a function from a file to another to make it works, I take it.
-
Ok, I see the problem. You are not doing what you think you are doing. You update the widtop(that you don't show and you leak) in your dialog which is not the widtop from your MainWindow.
Try with this:
@
dialog.h
{
// your stuffsignals:
void buttonClicked(); // << bad naming, only for test
}dialog.cpp
dialog::dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::dialog)
{
ui->setupUi(this);
connect(ui->pushButton, SIGNAL(clicked()), SIGNAL(buttonClicked()));
}widtop.cpp
void widtop::on_pushButton_clicked()
{
// you were creating a memory leak
// when using new, always have a corresponding delete
// UNLESS you're using Qt's parent/child system
dialog t;
t.setModal(false);
connect(&t, SIGNAL(buttonClicked()), SLOT(addItemFunction()));
t.exec();
}
@ -
You're welcome !
Indeed, and there's plenty of example that are useful to learn the basics. Not really strange, it's signal forwarding. You can use it to avoid tight coupling, like in this case, you would either need to have your dialog class know widtop or give widtop access to your dialog button to make the connection. Both of which are not clean design.