在Qt GUI设计器屏幕上,使用(转到SLOT),(选择已更改)选项生成回调“on_textEdit_selectionChanged”,并将3行代码放入该函数中,如下所示:
void MainWindow::on_textEdit_selectionChanged() {
QString msg = "in on_textEdit_selectionChanged";
qDebug() << msg ;
ui->textEdit->setText(msg);
}
当在textEdit框中单击鼠标触发上述函数时,将调试信息和DOES写入ui写入。
当通过不同回调*中的代码更改选择触发上述函数执行时,写入调试消息但不写入ui。
有人可以解释这种看似不一致的行为吗?
*选择在另一个成员函数中更改为“ui-&gt; textEdit-&gt; selectAll()”
答案 0 :(得分:0)
@infixed的调试行建议揭示了我的问题的根源。 &#34;这&#34;地址和&#34; ui&#34;当从gui回调调用我的成员函数回调时,地址是不同的。
qDebug() << "functionName this = " << this << "ui = " << ui;
怀疑我的SIGNAL / SLOT连接是问题但不确定如何修复它。我有:
connect (this, SIGNAL (showdata_signal()), this, SLOT(showdata_slot()));
在构造函数中:
void MainWindow::display_that_data(){ //public function called
qDebug() << "display_that_data this = " << this << "ui = " << ui;
emit showdata_signal();
}
void MainWindow::showdata_slot() {
QString msg = " in showdata_slot";
qDebug() << "showdata_slot this = " << this << "ui = " << ui;
msg = "manual selection change";
qDebug() << msg ;
ui->lineEdit_3->setText(msg);
ui->tlm_vals->selectAll();
msg = "manual selection done";
qDebug() << msg ;
}
void MainWindow::on_tlm_vals_selectionChanged() {
static int i =0;
QString msg = "in selectionChanged" + QString::number(i);
qDebug() << "selectionChanged this = " << this << "ui = " << ui;
qDebug() << msg ;
ui->lineEdit_2->setText(msg);
i++;
}