1
votes

I have a mainwindow it can do some search from MySQL, eg. if I enter "abc", it will search all email with "abc" inside, then it will insert all result to a QTableWidget and show it, if i double click any of it, it will create a QDialog by show() and pass that email I clicked by SIGNAL/SLOT, the problem is, i want to create multiple QDialog if necessary. By double clicking another email, it should create another QDialog, but every time I open a new QDialog, all email I pass in will change to the last one I clicked, hope someone can teach me whats going on.

Here is my mainwindow double click function

void MainWindow::on_tableWidget_cellDoubleClicked(int row, int column)
{
    auto resule = ui->tableWidget->item(row, 1);
    Dialog* dialog = new Dialog(this);
    connect(this, SIGNAL(sendTargetEmail(QString)), dialog, SLOT(receiveTargetEmail(QString)));
    dialog->show();
    emit sendTargetEmail(resule->text());
}

Here is my QDialog function

Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog){


    ui->setupUi(this);

    label1 = new QLabel("Email:", this);
    label1->setGeometry(10, 30, 50, 20);

    emailLabel = new QLabel(this);
    emailLabel->setGeometry(60, 30, 160, 20);
}

void Dialog::receiveTargetEmail(QString email){
    m_email = email ;
    emailLabel->setText(m_email);
}
1
explain yourself better I do not understand. read How to Askeyllanesc
Why do you use signals and slots with sendTargetEmail? if you are happening synchronously.eyllanesc
@eyllanesc Sorry for confuse, i wil try harder next time, about SIGNAL/SLOT beacuse i want to pass a specific email to dialog, i am planing do a full data display for that email in dialog.TANPAN

1 Answers

0
votes

All your dialog instances will be affected by connected signal/slot, the slot will be called for all previously created dialogs as well.

... .. so all your dialogs labels text will change to latest email string!

If you want to keep the signal/slot connection for some reason, then don't use the slot to set the label text, you can set the text directly to the instance:

Dialog* dialog = new Dialog(this);
connect(this, SIGNAL(sendTargetEmail(QString)), dialog, SLOT(receiveTargetEmail(QString)));
dialog->emailLabel->setText(resule);
dialog->show();
emit sendTargetEmail(resule);

and remove this line from your slot:

emailLabel->setText(m_email);

Edit

When you create a Dialog with new, you are dynamically creating that object. It will stay in memory unless it is explicitly destroyed or when its parent is destroyed. You are also making a connection to receiveTargetEmail(QString) every time you create an instance of Dialog. This connection is also permanent, unless that QDialog is destroyed.

Whenever you emit sedTargetEmail() the receiveTargetEmail(QString) slot of every instance of Dialog that you created earlier is being triggered. Instead of making a connection with SIGNAL/SLOT, you can directly call that function. This will make sure that only the last QDialog that you created is receiving the text.

void MainWindow::on_tableWidget_cellDoubleClicked(int row, int column)
{
    auto resule = ui->tableWidget->item(row, 1);
    Dialog* dialog = new Dialog(this);
    dialog->receiveTargetEmail(resule->text());
    dialog->show();
    emit sendTargetEmail(resule->text()); //you may have other things planned for the signal so you can leave it as is, depending on your design
}