Signal/Slots Problem



  • Hi there, it's my first post here. I already made some research about my problem but it seems I can't find an answer .

    Here's my issue. I got a mainwindow page that display other pages via a QWidget. Let me explain , my main window just have a logo and some buttons and I just add widget on this page to switch between my pages. All of that works but on the option page I have 4 buttons that allow me to switch to some pages you can't access on the mainwindow. I'm trying to make a signal on a button click and then go to a slot on my mainwindow page to close and change QWidget , like : connect(this,SIGNAL(buttonclicked()),"My mainwindow page,SLOT(My mainwindow slot)));

    If you have an idea on what should I do I'll be really happy. just let me know if you need to see my code 'cause it's a little big to show ^^


  • Moderators

    @Amaury

    QStackedWidget *stackedWidget = new QStackedWidget;
    int firstPageIdx =  stackedWidget->addWidget(firstPageWidget);
    int scondPageIdx =  stackedWidget->addWidget(secondPageWidget);
    int thirdPageIdx = stackedWidget->addWidget(thirdPageWidget);
    int fourthPageIdx = stackedWidget->addWidget(fourthPageWidget);
    
    QSignalMapper* signalMapper = new QSignalMapper( this );
    // button for page 1
    connect(buttonPage1, SIGNAL(clicked()), signalMapper, SLOT(map()));
    signalMapper->setMapping(buttonPage1, firstPageIdx );
    // button for page 2
    connect(buttonPage2, SIGNAL(clicked()), signalMapper, SLOT(map()));
    signalMapper->setMapping(buttonPage2, secondPageIdx );
    ...
    


  • Hi thank you for your answer but can you please tell me what the int correspond to and what to put in addWidget, I imagine that the int is just a variable and the addWidget is my page but should i create a pointer to my page ?



  • After some tries I just found that it can't work like that , I open a Qwidget Options from my MainWindow and then on the QWidget Options there is 4 button that should replace the option window. As i don't have those 4 buttons on my MainWindow I can't use a stackedWidget on my MainWindow page because there is no reference to the buttons. Do you see something that could help ?


  • Moderators

    @Amaury said in Signal/Slots Problem:

    Hi thank you for your answer but can you please tell me what the int correspond to and what to put in addWidget, I imagine that the int is just a variable and the addWidget is my page but should i create a pointer to my page ?

    see the corresponding Qt docs please

    @Amaury said in Signal/Slots Problem:

    After some tries I just found that it can't work like that , I open a Qwidget Options from my MainWindow and then on the QWidget Options there is 4 button that should replace the option window. As i don't have those 4 buttons on my MainWindow I can't use a stackedWidget on my MainWindow page because there is no reference to the buttons. Do you see something that could help ?

    this was just an example. It doesn't matter which object triggers the signal mapper.
    Either way make the buttons from the options page accessible.

    Or do another approach and add slots for each page to show (onShowPage1(), onShowPage2(),....) and connect to them from whatever widget you like.

    You can add 4 signals to your options page and connect the button signals to them.
    (It's also possible to connect signal-to-signal, not only signal-to-slot)


Log in to reply
 

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