简体   繁体   English

Qt连接两个信号和一个插槽

[英]Qt connecting two signals and one slot

I have a programm with a QLabel, QTextEdit and a QPushButton. 我有一个带有QLabel,QTextEdit和QPushButton的程序。

I want to put the text from LineEdit to Label when I click the button. 单击按钮时,我想将文本从LineEdit放置到Label。

I can do that by creating my own slot but can it be done with Qt slots? 我可以通过创建自己的插槽来做到这一点,但是可以使用Qt插槽吗?

I've tried this code but it works not as I want... 我已经尝试过此代码,但是它不能按我的意愿工作...

this->connect(pushButton ,SIGNAL(clicked()), lineEdit, SIGNAL(textChanged(QString)), Qt::QueuedConnection);
t->connect(lineEdit, SIGNAL(textChanged(QString)) , label ,SLOT(setText(QString)), Qt::DirectConnection);

If you need to force the user to push a QButton for "applying" the text he/she typed in a QTextEdit to a QLabel, maybe you want to check the validity of the inserted text, or use the text to achieve some goal or to store it in a variable for later use... so you need a custom slot or a custom class. 如果您需要强制用户按下QButton,以将他/她在QTextEdit中键入的文本“应用”到QLabel,则可能要检查插入文本的有效性,或使用该文本来实现某些目标或将其存储在变量中以备后用...因此您需要一个自定义插槽或自定义类。

Instead you can connect the signal QTextEdit.textChanged(QString) to the slot QLabel.setText(QString), so everything is typed in the QTextEdit is sent to the QLabel without pushing a button. 相反,您可以将信号QTextEdit.textChanged(QString)连接到插槽QLabel.setText(QString),以便将在QTextEdit中键入的所有内容发送到QLabel,而无需按下按钮。

But all depends on your aim. 但是,一切都取决于您的目标。

Here's how I would do it: 这是我的处理方式:

connect(ui->pushbutton, SIGNAL(clicked()), this, SLOT(slot_pushbutton_clicked()))

And then in the the slot_pushbutton_clicked slot, 然后在slot_pushbutton_clicked插槽中,

ui->label->setText(ui->lineEdit->text)

Hope it helps :) 希望能帮助到你 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM