Skip to content
  • 1 Votes
    1 Posts
    947 Views
    No one has replied
  • 0 Votes
    2 Posts
    991 Views
    JorgeJ

    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.

  • 0 Votes
    4 Posts
    1k Views
    SGaistS

    It really depends on your application. One thing to keep in mind is that these buttons should not know what's in e.g. your login widget. They should be enabled when appropriate. Otherwise you are going to have a pretty hard to maintain widget there if you add more "pages".