信号和插槽

时间:2017-01-31 06:28:41

标签: c++ qt signals-slots

我已经写了一些代码,如果我以客户身份登录,它应该隐藏列customer(该列的值为1)。我需要将参数传递给信号。我使用的customerLoginisSa。如何将这些参数传递给信号和插槽?

Main.cpp

int main(int argc, char *argv[])
  {
   QApplication a(argc, argv);

   initialize();

   SafeOTPWindow w;
   Login login;
   login.connect(&login, SIGNAL(loggedIn()), &w, SLOT(show())  );
   login.connect(&login, SIGNAL(loginCancelled()  ), &w, SLOT(close()) );

   login.show();
   a.exec();
}

Login.h

#ifndef LOGIN_H
#define LOGIN_H

#include <QDialog>

namespace Ui {
class Login;
}

class Login : public QDialog
{
    Q_OBJECT

public:
    explicit Login(QWidget *parent = 0);
    ~Login();
     bool isSa;

    signals:
    void loggedIn();
    void loginCancelled();

    private slots:
    void on_buttonBox_accepted();

    void on_buttonBox_rejected();

   private:
     Ui::Login *ui;
  }; 

 #endif // LOGIN_H

这是我的另一个文件,我需要隐藏列(Customer),OtpWindow.cpp

  void SafeOTPWindow::initLogTable()
    {

     QList<OtpLog> logs;
     int ret = otpRepo.fetchOtpLogs(logs);
     if( ret != errorCodes::SUCCESS )
     {
         QMessageBox msgBox(QMessageBox::Critical, QString("SafeOTP"),
                       QString("OTPLogs could not be fetched"),QMessageBox::Ok, this);
         msgBox.exec();
         QLOG_ERROR() << "fetchLogs error " << ret;
         return;
     }

     QStandardItemModel *model = new QStandardItemModel(0,5,this); //5 columns
     model->setHorizontalHeaderItem(0, new QStandardItem(QString("Date")));
     model->setHorizontalHeaderItem(1, new QStandardItem(QString("Customer")));
     model->setHorizontalHeaderItem(2, new QStandardItem(QString("Server")));
     model->setHorizontalHeaderItem(3, new QStandardItem(QString("Authorized by")));
     model->setHorizontalHeaderItem(4, new QStandardItem(QString("Description")));


       for(QList<OtpLog>::Iterator lIt = logs.begin(); lIt != logs.end(); lIt++)
        {
           OtpLog& log = *lIt;
           QList<QStandardItem*> row;
           row.push_back(new QStandardItem(log.when.toString("dd MMM yyyy, hh:mm")));
           row.push_back(new QStandardItem(QString(log.customer)));
           row.push_back(new QStandardItem(QString(log.server)));
           row.push_back(new QStandardItem(QString(log.author)));
           row.push_back(new QStandardItem(QString(log.reason)));

             model->appendRow(row);
        }
     // set the data model
     ui->tblLog->setModel(model);
     //Set the column to hide
     ui->tblLog->setColumnHidden(1,true);


    // set the column widths
    int tw = ui->tblLog->width() - 5;
    int w = tw / 6;

      for(int i=0; i<4;i++)
      {
        ui->tblLog->setColumnWidth(i,w);
        tw -= w;
      }
     ui->tblLog->setColumnWidth(4,tw);
   }

1 个答案:

答案 0 :(得分:0)

对于插槽/信号示例,您需要连接具有相同参数的两个对象。所以让我修改你的例子:

假设你有:

signals:
void loginStatusChanged(bool isLoggedIn);

在您的登录方法中,您将拥有以下内容:

bool status = checkLogin();
emit loginStatusChanged(status);

然后你应该在SafeOTPWindow中有一个专门的插槽来做类似的事情:

void SafeOTPWindow::UpdateTableAfterLogin(bool isLoggedIn)
{
  ui->tblLog->setColumnHidden(1,isLoggedIn);
}

然后像往常一样连接:

connect(&login, SIGNAL(loginStatusChanged()), &w, SLOT(UpdateTableAfterLogin(bool))  );