Communication between QWidgets in Stacked Window
-
Hello,
I'm new in Qt and I have a trouble that stop me for a week...Ok it's probably a stupid question but I tried different ways with no success.
I have to assemble some QWidget created by Qt Creator, the first one ( we call it "pag1", it is composed by "pag1.ui", "pag1.h" and "pag1.cpp") shows a window with some text,controls and a button "Go to Page 2", the second qWidget has text and controls.
Finally, we have a main window (MainWindow) with controls and a Stacked Window. By "promote" function I linked pag1 widget to Stacked Window first page (Page1) and pag2 to the second one (Page2).
I have no problem if I call pag1 and pag2 by main window, but I'm no able to make button "Go to Page 2" ordering Mainwindow to erase Page1 and show Page2.
Any solutions? I tried signals and slot (I think it's the correct way), but pag1 doesn't see pag2...
Thanks for all and good days!
Mario -
@lagodolio
Have you created pages using QStackedWidget http://doc.qt.io/qt-4.8/qstackedwidget.html ? -
Hello Ratzz and thanks for your support.
My problem is that every page is quite complex ( data analysis and processing / system control /database management ). A friend of mine and I are creating autonomous programs so we are able to work quite independently.
Thanks to qt library we are going to assemble those parts in GUI style ( we initially used Processing, but we abandon it because of performance).
Those programs (they appear as stacked pages) have to call each other, just like "pag1" of my little sample calls pag2.
Thank you and happy weekend to all the forum!
Mario -
@lagodolio
hi
Are you asking how you can access widgets from one page to another page ?private:
Ui::pag1 *ui;or what do you mean by
"..have to call each other," ?If each page is a module, you should define public signals for the processing result.
You can then connect page1 to page2 for piping.in your sample the pages are QMainWindows.
you put them in stacked widget?? -
Sorry , to be honest "..have to call each other" is not very clear... It means that some widgets promoted in stacked windows have a button that call another widget in stacked windows.
My sample pag1.cpp should be something as:// /* pag1.cpp*/ #include "pag1.h" #include "ui_pag1.h" pag1::pag1(QWidget *parent) : QMainWindow(parent), ui(new Ui::pag1) { ui->setupUi(this); } pag1::~pag1() { delete ui; } void pag1::on_pushButton_clicked() { /******************************************** Do something as: ui->pag1->>setHidden(true); ui->pag2>>setHidden(false); **********************************************/ }
-
Hi
you cannot talk to other pages from a page
that would become spaghetti code if all pages must know all other pages.What you can do is to to let mainwindow control the pages.
since it already have the stacked widget.So you use signals and slot to make this happen.
I have altered your sample to do this:
https://www.dropbox.com/s/elj1zqu9ts73hur/stk.zip?dl=0The change are:
Define signal for page
emit ShowPage( 1 ); from buttonin mainwindow
make slot
connect( ui->page, SIGNAL(ShowPage(int)), this, SLOT(ShowPage(int)) );and
// this is called from pages via signal
void MainWindow::ShowPage(int index) {
qDebug() << "show page request: " << index;
// do the actual switch
ui->stackedWidget->setCurrentIndex(index);
}Now a page can ask to switch to other page.
You can do this for any other function or info you want between pages. -
@lagodolio
super :)just a note:
If you design a base class for pages and inherit from that, you can
just define the signals for showpage, and what else you need , in
this base class and all pages can be hooked up pr default.