2

我正在尝试跟踪少数 QTextEdits 的 textChanged() 信号。无论文本编辑是否发出信号,我都想做同样的事情:如果 QListWidget 为空,则取消选中其关联的复选框,否则保持选中状态。我的功能如下:

void MainWindow::changed()
{
    QString tempStr = ui->hNMRedit->toPlainText();
    if(tempStr != "")
    {
       ui->checkList->item(0)->setCheckState(Qt::Checked);
    }
    else
    {
       ui->checkList->item(0)->setCheckState(Qt::Unchecked);
    }
}

使用当前的方法,我必须为每个 QTextEdit 创建一个这样的函数;每个函数都包含几乎相同的代码。如果我将每个文本编辑存储在一个数组中(这样我就可以在 QListWidget 中找到它们的关联索引),我是否有可能拥有这样的插槽?

void MainWindow::changed(QWidget *sender)   // for whichever text edit emits the
                                            // textChanged() signal
{
    QString tempStr = sender->toPlainText();
    if(tempStr != "")
    {
       // I would potentially use some sort of indexOf(sender) function on the array I 
       // mentioned earlier here... a little new to Qt, sorry
       ui->checkList->item(array.indexOf(sender))->setCheckState(Qt::Checked);
    }
    else
    {
       // same as above...
       ui->checkList->item(array.indexOf(sender))->setCheckState(Qt::Unchecked);
    }
}

这是可能的还是我应该为每个文本编辑创建一个单独的插槽?如果需要进一步澄清,请告诉我!

最后,我觉得 QLineEdits 和 QTextEdits 之间唯一有意义的区别是默认大小。为了保持一致,我是否应该在整个 UI 中只使用这些对象之一?

谢谢!!!

4

2 回答 2

3

I think you are missing the point of slots and signals. How are you creating the connections? Are you trying to check a box when any of the text boxes change? If so use a QSignalMapper to map the textChanged() signals to send a value of true and connect that to the QCheckBox setChecked(bool) slot.

If that is too complicated subclass QCheckBox and create a set of functions checkBox() uncheckBox() so you can toggle states without a variable. Then connect the QTextEdit textChanged() to your subclass checkBox()

If this is not what you are looking for, at least subclass QTextEditto take in a QCheckBox that it can change when the text changes instead of duplicating code for every QTextEdit

于 2010-01-30T21:33:49.667 回答
0

您所需要的只是一个 的散列QAbstractButton*,由 键控QTextEdit*。在插槽中,您sender()在哈希中查找 ,如果找到您需要的按钮。这正是由 s 所做的QSignalMapper:您可以从发送者映射QWidget*到您的按钮QWidget*。用于qobject_cast投射到QAbstractButton*.

于 2012-05-07T19:22:57.103 回答