Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] How to access a slot from a different widget: If form1 clicks a button, form2 should respond.

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

Scheduled Pinned Locked Moved General and Desktop
qstackedlayoutqstackedwidgetqstackedwidgetslayout
2 Posts 1 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jorge
    wrote on 10 Jun 2015, 16:59 last edited by Jorge 6 Dec 2015, 18:52
    #1

    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.

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jorge
      wrote on 10 Jun 2015, 19:20 last edited by
      #2

      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.

      1 Reply Last reply
      0

      1/2

      10 Jun 2015, 16:59

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved