views:

371

answers:

1

I'm trying to keep track of the textChanged() signal on for handful of QTextEdits. I want to do the same thing regardless of the text edit emitting the signal: uncheck its associated checkbox in a QListWidget if it becomes empty and leave it checked otherwise. The function I have so for is as follows:

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);
    }
}

With the current approach, I would have to make a function like this for every QTextEdit; each function containing virtually identical code. If I stored each of the text edits in an array (so I could find their associated index in the QListWidget), would it be possible for me to have a slot like this?

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);
    }
}

Is this possible or should I just create a separate slot for every text edit? Please let me know if any further clarification is needed!

Lastly, I feel like the only meaningful difference between QLineEdits and QTextEdits is the default size. In favor of keeping things consistent, should I just use one of these objects throughout my UI?

Thanks!!!

+2  A: 

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

Adam W
I agree about QSignalMapper... That will make the array-based management much easier.
Caleb Huitt - cjhuitt
Sounds good, I'll give the QSignalMapper a try. The only difficult part is the QCheckBoxes are in a QListWidget... I don't know if I'll be able to connect the checked slot like you described. Do you still think it will be able to work?There are also some QLineEdits I would like to connect to the single slot I mentioned earlier. As a matter of convenience, should I just change them all to QTextEdits (so I can store them in the array and use the same function on the recieving object)?
synchronicity
`QCheckBoxes`: Yes, either at the time of creation or afterward, you have access to them. If you try to access them after you put them into the list, you either need to keep the pointer or cast them after calling one of the get item functions.`QLineEdits`: You shouldn't have to, store them as `QWidget` pointers and cast them out when you need specific functionality.
Adam W
Adam, thanks so much for your help. My last question has been added to my original post so the code is readable...
synchronicity
I think your setMapping call is incorrect, the parameters should be textEdits[i] not edit. Unless edit is something else I am missing, it's a little hard to read in comment format.
Adam W
The code is in a more readable format in my original post if you want to take a look - I recently edited it to include the code mentioned. Sorry about that, forgot to change the parameters. The signal mapper still won't connect each textEdit to the proper slot. Do I need to add an additional connect statement or something?
synchronicity
I don't see the QSignalMapper code anywhere now. The process is connect signal to signal mapper. Set mapping on signal mapper. Connect signal mapper mapped signal to receiving slot. So there should be 2 connects per pairing now.
Adam W