muktoshuvro
muktoshuvro

Reputation: 438

How to pass value from one SLOT to another SLOT

This question is absolutely a newbie question so I apologize for that. I have a SLOT which pretty much looks like this.

 void MainWindow::on_actionSelect_for_hashing_triggered()
 {

 QFile file(QFileDialog::getOpenFileName (this, tr("Open File"),
                                         "",tr("")));

     if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
         return;  
     QByteArray line = file.readAll();  
 }

Now I want to pass line to my another SLOT which is look like this..

 void MainWindow::on_pushButton_clicked()
 {
  line2 = line;  // QByteArray line2 has been assigned globally
 qDebug()<<line2;
  }

So here I simply want to print line2 which will receive value from line from first SLOT. How might I do that ?

Upvotes: 0

Views: 973

Answers (1)

cmannett85
cmannett85

Reputation: 22346

void MainWindow::on_actionSelect_for_hashing_triggered()
{

    QFile file(QFileDialog::getOpenFileName (this, tr("Open File"), "",tr("")));

    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;  
    QByteArray line = file.readAll();  
    on_pushButton_clicked( line );
}

void MainWindow::on_pushButton_clicked( const QByteArray& line )
{
    line2 = line;  // QByteArray line2 has been assigned globally
    qDebug()<<line2;
}

Just call the method and pass the byte array. If you need an on_pushButton_clicked(), then just overload or provide a default argument.

If you want to be able to connect/disconnect them at runtime, you will have to get on_actionSelect_for_hashing_triggered() to emit something that on_pushButton_clicked(..) can receive.

And I'm going to give the usual speech on not using global variables...

Upvotes: 4

Related Questions