Connect Custon Widget with MainWindow



  • Hi people, I made this question in the Spanish section, but I need more feedbak...

    I'm working in an application, I have several custom widgets and they are showing in the MainWindow's Central Widget. The child widget made one check and it must send the result to Statusbar in the Mainwindow. I'm trying with SIGNAL and SLOTS, but I loss...

    The code:

    inicio.h

    public slots:
        void changeconexionstatus(const bool &statuscon);
    

    inicio.cpp

    void inicio::terminal(){
       prueba *open = new prueba(this);
       connect(open,&prueba::statusconexion,this,&inicio::changeconexionstatus);
       this->setCentralWidget(open);
    }
    
    void inicio::changeconexionstatus (const bool &statuscon){
        if (statuscon){
            statusconico->setPixmap(QPixmap(":/img/img/connect.png"));
        }
    }
    

    prueba.h

    signals:
        void statusconexion(const bool &value);
    

    prueba.cpp

    connect(ui->pushButton,&QPushButton::clicked,this,check_connection);
    
    void tpv::check_connection(){
            emit statusconexion(true);
    }
    

    With this code if I clicked the button the SIGNAL is emitted, but I need that the SIGNAL is sent when the function check_conection finish run... If I call directly to the function the SIGNAL is not emitted.

    Thanks in advance

    Regards
    Raul


  • Lifetime Qt Champion

    Hi,

    From your description, it seems that you should call emit at the end of check_connection. So it's not really clear what your problem is.



  • I think that I know where is my problem... but I don't know who I can solve for the moment :-(.

    I think that the problem is in the run time line.... when tpv *open = new tpv(this) the custom widget is load and the check_connection emit the signal but the connection in the main window connect(open,&prueba::statusconexion,this,&inicio::changeconexionstatus); is not active yet and then nothing change in the Mainwindow.

    For this reason when I send signal with a pushbutton in the custom widget it work, because the custom widget is loaded completely and the connection in the main window created.


  • Moderators

    @raulgg Do you mean you call check_connection() in the tpv constructor?
    If so, then you can simply remove that call from constructor and call it outside:

    tpv *open = new tpv(this);
    connect(open,&prueba::statusconexion,this,&inicio::changeconexionstatus);
    open->check_connection();
    


  • @jsulm said in Connect Custon Widget with MainWindow:

    @raulgg Do you mean you call check_connection() in the tpv constructor?
    If so, then you can simply remove that call from constructor and call it outside:

    tpv *open = new tpv(this);
    connect(open,&prueba::statusconexion,this,&inicio::changeconexionstatus);
    open->check_connection();
    

    Great!!! I look stupid, this is the simple solution:-)

    Thank so much for your support @jsulm


Log in to reply
 

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