PDA

View Full Version : Qtablewidget Combobox



aekilic
18th July 2007, 08:18
Dear All

We have set up a net Qtablewidget, and for the table we have many rows and many columns. Columns quantity are allways the same but the row quantities are changing according to the sql record.

For the table we could lile to put a combobox inside one cell, we have used


table->setCellWidget(row_of_table, 1, combo);
row_of_table = row_of_table + 1;

in a while function. But the combo always appiers in the last row.

Could any body help us with this!

Take care

Michiel
18th July 2007, 09:27
Hm. I'm not exactly sure how to fix this for a QTableWidget (though there is definitely a way), but I suggest that you forget about QTableWidget and take a look at QTableView and QSqlTableModel. They are part of the model/view framework and specifically meant for this sort of thing. Using custom widgets in certain columns will be easier that way.

aekilic
18th July 2007, 10:48
That really didnt halp...:(

We know the differances between the model and the item. We are just bring the data by a sql quary. The only problem is combobox does not apper in all the rows. İt just appers in a singe row.

jpn
18th July 2007, 11:15
Why copy data from QSqlQuery to QTableWidgetItems when you could use QSqlQueryModel and QTableView? I suggest you take a look at the spinbox delegate example (http://doc.trolltech.com/4.3/itemviews-spinboxdelegate.html) to get an idea how to provide custom editors.

Edit: As Michiel noticed, actually QSqlTableModel for an editable model of course.. :)

Michiel
18th July 2007, 11:16
Please be more specific about your problem. If you initiate an edit on one of the earlier rows (double-click), does the editor appear there then? Maybe you want all rows to have a visible combobox at the same time (not just during edit)? Then you want to open a persistent editor. See QTableWidget::openPersistentEditor(). If you do that, I think you also need a separate widget for each row. You can't use the same one for all of them at the same time.

If that's not what you mean, please explain more clearly.

And I still think you might want to use the Qt model/view framework instead of a QTableWidget. It's far more natural and robust that way. There's a model class specifically for Sql tables (QSqlTableModel).

rajesh
18th July 2007, 11:16
Have you put your code in a loop?

aekilic
18th July 2007, 13:54
Firstly, we normaly use models views istdead of item views. But we could use in this time, because every cell has an different behavier and also ever cells quaries from other cells. So we tought that it should be more easy to use item view.

For our problem, we SELECT make a quary like this,

void slottable()
{
row_of_table = 0;
id = "2";

QComboBox *newcombo;
newcombo = new QComboBox(this);

QSqlQuery Pindex;
Proformaindex.exec("SELECT ... FROM.... WHERE p_id = '" + id + "' ");

QSqlQuery kod;
kod.exec("SELECT id, kod FROM stk_kod");
while(stok_kod.next()) newcombo->addItem(kod.value(1).toString(), kod.value(0).toInt());

while(Pindex.next())
{

tableP->insertRow(row_of_table);

QTableWidgetItem *pr_id = new TableWidgetItem(Pindex.value(0).toString());
QTableWidgetItem *prof_id = new QTableWidgetItem(Pindex.value(1).toString());
QTableWidgetItem *profindex_turu = new QTableWidgetItem(Pindex.value(2).toString());
.
.
.
.
tableP->setItem(row_of_table, 0, pr_id);
tableP->setItem(row_of_table, 1, prof_id);
tableP->setItem(row_of_table, 2, profindex_turu);
.
tableProformaindex->setCellWidget(row_of_table, 1, newcombo);

row_of_table = row_of_table + 1;
}
tableP->resizeColumnToContents(0);
tableP->resizeColumnToContents(1);
}

The code is like this in simple, we would like to combo box to come when we would like to edit it. But acctually it comes to the last row and never goes from there.

Any way this what i could not do, if you could help me and save me from this desert i would be so happy!

Michiel
18th July 2007, 14:20
Now that I think about it, it's quite possible that the QTableWidget::setCellWidget() function makes the specified cell the new (and only) owner of the specified widget. This would explain why only the last row uses it.

So maybe you should only call that function when an editor is requested.

jpn
18th July 2007, 14:22
Has the table been made sortable?

aekilic
18th July 2007, 14:36
Dear Michiel

How this is possible with the codes? And the is going to be other combo boxes other than this one?

Dear Jpn

No it not sortable!

Michiel
18th July 2007, 14:45
How this is possible with the codes?

I'm not really sure. Maybe you could reimplement QAbstractItemView::edit(). My first try would be to first set the cell widget and then call QAbstractItemView::edit(). But I've never tried this. I may be way off on this one.

Experiment a little. Search the docs.


And the is going to be other combo boxes other than this one?

Sorry, I don't know what you mean.

aekilic
18th July 2007, 15:11
I was telling there are going to be more combo boxes.

Michiel
18th July 2007, 15:15
Ah, but you used a question mark. :)

There will be more comboboxes. But, I assume, not at the same time. Only when an editor is requested (by a double-click trigger). Right?

aekilic
18th July 2007, 15:40
Yes, I would like to have the combo when i double clicked the item in the table!

jpn
18th July 2007, 15:45
Yes, I would like to have the combo when i double clicked the item in the table!
Take a look at the spinbox delegate example as already suggested earlier in this thread.

aekilic
21st July 2007, 08:09
Dear All

Actually we have solved problem in a different way and we have added the combo different way. Thank you very much that you have helped us!

AviMittal
30th July 2009, 08:28
Can you please share how you solved this problem....thanks

faisal ali
7th July 2015, 11:22
try this you have to make a new object everytime.


for(int i=0; i<n; i++)
{
QComboBox * editor = new QComboBox(parent);
editor->addItem("0");
editor->addItem("1");
editor->addItem("Z");
editor->addItem("Last Known State");
io_ui->tableWidget->setCellWidget(i,j,editor);
}