Communication between QWidgets in Stacked Window
-
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.