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 ?



  • Yes, I created a QStackedWidget by Qt Creator. I can see it in mainwindows.ui, but it is not editable...



  • @lagodolio
    can you create a minimal sample to reproduce it?



  • @Ratzz Yes, of course.
    As my project, this sample is created by Qt Creator. The program is called by inter_comm.pro.
    Thank you in advance and have a nice weekend,
    Mario

    This is the zip file - fully editable



  • @lagodolio
    You need not create two new pages separately rather you can use 2 stack pages to achieve this.



  • 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


  • Qt Champions 2016

    @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);
    **********************************************/    
      
    }
    
    

  • Qt Champions 2016

    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=0

    The change are:
    Define signal for page
    emit ShowPage( 1 ); from button

    in 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.



  • Wow, it works!
    You are right, this approach is more efficient ( and elegant too) rather then my code
    Thanks for all, now I can start working ( after a week ...).
    Have a nice days.
    Mario


  • Qt Champions 2016

    @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.



  • Perfect !


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.