[SOLVED] How to access a slot from a different widget: If form1 clicks a button, form2 should respond.



  • Hi,
    I'm using two widgets in a stacked layout. the first one, mainWidget, is the one with said layout and therefore is the one in charge of "movement", meaning that it should receive a signal from is stacked widgets that the currentIndex should change.
    The second widget, which is a login form, verifies the user and password from a SQLite database. The verification part is already done, although I still can't figure out how to tell the mainWidget (the one that controls the stackedlayout) that it should change the index.
    To be clear, a button in the login form should be able to change the stack in mainWidget, but I couldn't do this either with accessors or connect().
    Is there anyway to do this?
    Thanks in advance.



  • In case anyone wonders, I found the way to connect both classes and tell mainWidget to change stack through the login widget's button.

    //header of the login widget
    public slots:
        void verifyUser(int iUserCorrect )
        {
            if ( iUserCorrect != iUserVerified )
            {
                iUserVerified = iUserCorrect;
                emit valueChanged( iUserVerified );
            }
        }
    
    signals:
        void valueChanged( int iUserCorrect );
    
    private slots:
        void on_btnLogin_clicked();
    
    private:
        Ui::QLogin *ui;
        QSqlDatabase sqlUsuarios;
        int iUserVerified;
    

    source file of the login widget (only where valueChanged was used, in the SQLite verification):

    iUserVerified = 0;
    
        QSqlQuery sqlQry;
        if(sqlQry.exec("SELECT User, Password, Role FROM Usuarios WHERE User=\'" + sUsername +
                "\' AND Password =\'" + sPassword + "\'"))
        {
            if(sqlQry.next())
            {
                ui->lblStatus->setText("Nombre de usuario y contraseña válidos.");
                qDebug() << "Usuario OK";
                verifyUser(1);
            }
    

    and finally, the connect() in the mainWidget source file. Note that I had already made an instance of a promoted login widget inside this file:

    #include "mainwidget.h"
    #include "ui_mainwidget.h"
    #include <qlogin.h>
    #include <QMessageBox>
    #include <QCalendarWidget>
    #include <QDebug>
    #include <QPushButton>
    
    mainWidget::mainWidget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::mainWidget)
    {
        mainLayout = new QVBoxLayout();
    
        stackedLayout = new QStackedLayout();
    
        QLogin *loginWidget = new QLogin;
    
        stackedLayout->addWidget(loginWidget);
    
        stackedLayout->addWidget(new QCalendarWidget);
    
        mainLayout->addLayout(stackedLayout);
    
        setLayout(mainLayout);
    
        connect(loginWidget, SIGNAL(valueChanged(int)),
                 this, SLOT(changeStack()));
    
        ui->setupUi(this);
    }
    

    The changeStack method simply changes the index of the stack:

    void mainWidget::changeStack()
    {
        stackedLayout->setCurrentIndex(stackedLayout->currentIndex() + 1);
        qDebug() << "changeStack()";
    }
    

    Took me some time, but I finally got it.



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