PDA

View Full Version : QComboBoxes in QTableWidget : which is focused ?



Nyphel
11th April 2007, 14:12
Hello :)

I have a QTableWidget that is composed of 4 columns and several rows.
2 columns are filled with QTableWidgetItems, it's just text.
2 columns are filled with QComboBoxes.

So, when I want to add a row in my table, I use the setItem function (http://doc.trolltech.com/4.2/qtablewidget.html#setItem) in order to add the 2 QTableWidgetItems. That's ok.
After that I need to add the 2 QComboBoxes, and I do it with the setCellWidget function (http://doc.trolltech.com/4.2/qtablewidget.html#setCellWidget). That's ok too.

My problem is that I need to know which cell has the focus when the user clicks on a button. So I've connected the signal clicked() (from the button) to a slot. The slot loops over the table and look at each row/colum in order to know if the cell has the focus.

For a given cell, I can know if it has the focus doing this :
- if its colum number = 0 or 1 : the cell is a QTableWidgetItem and I can test with :

if ((table->item(row, column))->isSelected() == true)
- if its colum number = 2 or 3 : the cell is a QComboBox and I can test with :

if ((table->cellWidget(row, column))->hasFocus() == true)

The matter is that the second test is never TRUE.
I've no compilation errors.
If I replace the QComboBoxes by QListWidgets, the test is OK.

I don't understand what's the problem ^_^.
Could you help me, please ?



EDIT : I think that the QComboBoxes lost focus when the user clic the button :mad:
I'm under Win2000. I'll try to use signal/slots to detect the last cell that had the focus.

I don't find any QTableWidget signal that is emitted when I clic a cell that contains a QComboBox :o. I don't know what to do :-/
I could use currentRow() and currentColumn to know the current object positon, but currentColumn doesn't care about the QComboBoxes in my cells... Unlike currentRow() :crying:.



.

smacchia
11th April 2007, 16:55
EDIT : I think that the QComboBoxes lost focus when the user clic the button :mad:
I'm under Win2000. I'll try to use signal/slots to detect the last cell that had the focus.

I don't find any QTableWidget signal that is emitted when I clic a cell that contains a QComboBox :o. I don't know what to do :-/
I could use currentRow() and currentColumn to know the current object positon, but currentColumn doesn't care about the QComboBoxes in my cells... Unlike currentRow() :crying:.

You can connect to one of the slots for QComboBox (like activated), or you could use a QComboBox derivation and re-implement the focusInEvent handler, perhaps emitting your own signal.

The alternative is to develop your own delegate and process focus in events in the event filter.

HTH,
Susan

Nyphel
12th April 2007, 08:08
Hi,

I can't really use the QComboBox signals/slots, because the user can add rows to my table... And this operation adds QComboBoxes in the cells. Like I don't store them in memory, using vectors for example, I can't say that the signal is connected to each QComboBox.

This is the reason why I would like to detect a focus change : I could go thrut the table and look at each cell in order to know the one that is focused.

jpn
12th April 2007, 14:07
What kind of data does the combo box contain? I'd suggest using a custom delegate (http://doc.trolltech.com/4.2/model-view-delegate.html) or an item editor factory (http://doc.trolltech.com/4.2/qitemeditorfactory.html) to provide the combo box as a custom editor (http://doc.trolltech.com/4.2/model-view-delegate.html#providing-an-editor) instead of using cell widgets.

Nyphel
13th April 2007, 10:16
I only store text in the QComboBoxes :)

That seems very interesting JPN, and there are examples too :D
Thanks a lot, I will give a big look !