QTableWidget and QComboBox, issues when changing the QComboBox content
-
Hi there,
I've added a QComboBox inside a QTableWidget, I need to process data every time this QComboBox content changes. There are a lot of these QComboBox in the QTableWidget.
What I have done so far:
- Used the QTableWidget SIGNALS, without success. Seems like they properly work only on QTableWidgetItems.
- Tried to connect the editTextChanged SIGNAL, worked but I need to know the row of the item. When I change the content using MouseWheel for example the table's current row does not change. The content of these QComboBox may be the same, so I can't search.
- Tried to install the event filter, but none seems to work when I confirm a new index for the QComboBox.
- Added a property to each of these dynamic items with their row position, helped on the event filter, but I had to disable the ordination system.
I'll try to sync all of what I've done to make it work.
Does anyone know a better way to do this? -
How did you add the combo boxes? Did you use the setTableWidget?
I would prefer using a QStyledItemDelegate subclass and use custom editors there (e.g. a combo box). That means, if you edit the table, a combo box appears in the edited cell and you can change the values.
-
I used setCellWidget.
What is this setTableWidget? I couldn't find any reference for it. (with a fast google search)I haven't used this class QStyledItemDelegate yet, your idea sounds interesting. Is there any example that you could show me?
A temporary solution that I've managed is to use setCurrentCell when the QEvent::FocusIn event is filtered.
Thanks for your answer!
-
[quote author="Cayan" date="1314648789"]I used setCellWidget.
What is this setTableWidget? I couldn't find any reference for it. (with a fast google search)
[/quote]Sorry, I meant setCellWidget :-)
[quote author="Cayan" date="1314648789"]I haven't used this class QStyledItemDelegate yet, your idea sounds interesting. Is there any example that you could show me?[/quote]
You can have a look at the "description of Model-View":http://doc.qt.nokia.com/4.7/model-view-programming.html#delegate-classes or at the "examples of Model-View":http://doc.qt.nokia.com/4.7/examples-itemviews.html or the "SpinBox Delegate example":http://doc.qt.nokia.com/4.7/itemviews-spinboxdelegate.html
The examples always use a view class but the QTableWidget is a derived class of QTableView, so the delegates will also work there
-
[quote author="Cayan" date="1314648789"]I used setCellWidget.
What is this setTableWidget? I couldn't find any reference for it. (with a fast google search)[/quote]In this case you create a QComboBox, so I'd just connect a signal of that combobox to some slot.
-
I tried to use the Model View system to make it work, but it gave me more headaches than solutions.
QTableView::setModel is private so I couldn't make it work with the table I had.Now it is working, what I've done? Filtered the QEvent::FocusIn to force the Cell selection, here goes the code:
@bool Dialog::eventFilter(QObject object, QEvent event)
{
if(object->isWidgetType() && event->type() == QEvent::FocusIn)
{
QWidget widget = qobject_cast<QWidget>(object);
if(widget)
{
ui->table->setCurrentCell(widget->pos().y() / 30, 0);
}
}return false;
}@
I'm using that stupid way to select the cell because it was not working with "the proper way". The cells height are constant so a temporary solution for a short on time project.
I'm glad this is working, but I found what I've considered an error, maybe from Qt.
I created a topic to discuss it: "link":http://developer.qt.nokia.com/forums/viewthread/9323/.