August 29, 2011

Cayan Cayan
Lab Rat
40 posts

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?

5 replies

August 29, 2011

Gerolf Gerolf
Robot Herder
3287 posts

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.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

August 29, 2011

Cayan Cayan
Lab Rat
40 posts

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!

August 29, 2011

Gerolf Gerolf
Robot Herder
3287 posts
Cayan wrote:
I used setCellWidget. What is this setTableWidget? I couldn’t find any reference for it. (with a fast google search)

Sorry, I meant setCellWidget :-)

Cayan wrote:
I haven’t used this class QStyledItemDelegate yet, your idea sounds interesting. Is there any example that you could show me?

You can have a look at the description of Model-View [doc.qt.nokia.com] or at the examples of Model-View [doc.qt.nokia.com] or the SpinBox Delegate example [doc.qt.nokia.com]

The examples always use a view class but the QTableWidget is a derived class of QTableView, so the delegates will also work there

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

September 1, 2011

Volker Volker
Ant Farmer
5428 posts
Cayan wrote:
I used setCellWidget. What is this setTableWidget? I couldn’t find any reference for it. (with a fast google search)

In this case you create a QComboBox, so I’d just connect a signal of that combobox to some slot.

September 1, 2011

Cayan Cayan
Lab Rat
40 posts

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:

  1. bool Dialog::eventFilter(QObject *object, QEvent *event)
  2. {
  3.     if(object->isWidgetType() && event->type() == QEvent::FocusIn)
  4.     {
  5.         QWidget* widget = qobject_cast<QWidget*>(object);
  6.         if(widget)
  7.         {
  8.             ui->table->setCurrentCell(widget->pos().y() / 30, 0);
  9.         }
  10.     }
  11.  
  12.     return false;
  13. }

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 [developer.qt.nokia.com].

 
  ‹‹ QGraphicsItem screenshot      [Solved] how to code a .pro file to compile 2 applications with 2 different target folders ››

You must log in to post a reply. Not a member yet? Register here!