Results 1 to 18 of 18

Thread: Qtablewidget Combobox

  1. #1
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Qtablewidget Combobox

    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

  2. #2
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Qtablewidget Combobox

    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.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  3. #3
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qtablewidget Combobox

    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.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Qtablewidget Combobox

    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 to get an idea how to provide custom editors.

    Edit: As Michiel noticed, actually QSqlTableModel for an editable model of course..
    Last edited by jpn; 18th July 2007 at 13:29.
    J-P Nurmi

  5. #5
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Qtablewidget Combobox

    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).
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  6. #6
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Qtablewidget Combobox

    Have you put your code in a loop?

  7. #7
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qtablewidget Combobox

    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!

  8. #8
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Qtablewidget Combobox

    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.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  9. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Qtablewidget Combobox

    Has the table been made sortable?
    J-P Nurmi

  10. #10
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qtablewidget Combobox

    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!

  11. #11
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Qtablewidget Combobox

    Quote Originally Posted by aekilic View Post
    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.

    Quote Originally Posted by aekilic View Post
    And the is going to be other combo boxes other than this one?
    Sorry, I don't know what you mean.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  12. #12
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qtablewidget Combobox

    I was telling there are going to be more combo boxes.

  13. #13
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Qtablewidget Combobox

    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?
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  14. The following user says thank you to Michiel for this useful post:

    aekilic (21st July 2007)

  15. #14
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qtablewidget Combobox

    Yes, I would like to have the combo when i double clicked the item in the table!

  16. #15
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Qtablewidget Combobox

    Quote Originally Posted by aekilic View Post
    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.
    J-P Nurmi

  17. The following user says thank you to jpn for this useful post:

    aekilic (21st July 2007)

  18. #16
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qtablewidget Combobox

    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!

  19. #17
    Join Date
    May 2009
    Posts
    31
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qtablewidget Combobox

    Can you please share how you solved this problem....thanks

  20. #18
    Join Date
    Jul 2015
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Cool Re: Qtablewidget Combobox

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

Similar Threads

  1. QTableWidget issues
    By Djony in forum Qt Programming
    Replies: 42
    Last Post: 20th December 2006, 00:27
  2. print QTableWidget
    By chak_med in forum Qt Programming
    Replies: 3
    Last Post: 4th November 2006, 19:46
  3. QTableWidget editing question
    By Trasmeister in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2006, 19:46
  4. Filling combobox from database
    By Philip_Anselmo in forum Qt Programming
    Replies: 3
    Last Post: 11th May 2006, 18:53
  5. Replies: 6
    Last Post: 5th March 2006, 22:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.