Results 1 to 5 of 5

Thread: Must all cellWidgets in a QTable be the same widget type?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Must all cellWidgets in a QTable be the same widget type?

    I have a QTableWidget with QComboBoxs in row 0. External events will trigger changes to row zero--I was going to copy row 0 using the data associated with each comboboxes currentItem into a new row in the table.

    Right now, I get the QTable created with row 0 having the ComboBoxes. I can addItems for each combobox when the external event occurs with no issues.

    However, I wanted one of the columns to NOT have a QComboBox, but just a regular cell. But, when I don't do a setCellWidget on column 0, I get an exception when trying to add an item to the column 1 combobox.

    I tried using a QLabel for the cell widget for the column 0, row 0 cell and have the same problem.

    Is this expected? Maybe if one cell has a widget, all cells in the table must use the same widget?

    Here are the relevant code snippets.

    In constructor:

    Qt Code:
    1. // No widget desired in column 0
    2. runOrder->setItem(0,0, new QTableWidgetItem);
    3. //runOrder->setCellWidget(0,0,new QLabel);
    4.  
    5. // Comboboxes in columns 1 through seven of row 0
    6. for (int i=1;i<7;i++)
    7. {
    8. QComboBox *qb = new QComboBox;
    9. runOrder->setItem(0,i, new QTableWidgetItem);
    10. runOrder->setCellWidget(0,i, qb);
    11. connect(qb, SIGNAL(currentIndexChanged(int)), this,SLOT(check_lineup(int)));
    12.  
    13. }
    14.  
    15.  
    16. // Comboboxes in columns 1 through seven of row 0, not column 0
    17. for (int i=1;i<7;i++)
    18. {
    19. QComboBox *qb = new QComboBox;
    20. runOrder->setItem(0,i, new QTableWidgetItem);
    21. runOrder->setCellWidget(0,i, qb);
    22. connect(qb, SIGNAL(currentIndexChanged(int)), this,SLOT(check_lineup(int)));
    23.  
    24. }
    To copy to clipboard, switch view to plain text mode 
    In the code that adds entries to the comboboxes:
    Qt Code:
    1. //Loop through the columns of row zero that have comboBoxes
    2.  
    3. for (int i=1;i<5;i++)
    4. {
    5. QComboBox * qb = dynamic_cast<QComboBox *>(runOrder->cellWidget(0,i));
    6. qb->clear();
    7. //Loop through the lineup
    8. for (int j=0;j<5;j++)
    9. {
    10. QString dog_name= lineup_query.value(j).toString();
    11. qb->addItem(dog_name);
    12. }
    13. qb->setCurrentIndex(i);
    14.  
    15. }
    16.  
    17. If I change
    18.  
    19. // Comboboxes in columns 1 through seven of row 0
    20. for (int i=1;i<7;i++)
    21. {
    22. QComboBox *qb = new QComboBox;
    23. runOrder->setItem(0,i, new QTableWidgetItem);
    24. runOrder->setCellWidget(0,i, qb);
    25. connect(qb, SIGNAL(currentIndexChanged(int)), this,SLOT(check_lineup(int)));
    26.  
    27. }
    To copy to clipboard, switch view to plain text mode 
    to loop from 0 to 7, everything seems to work fine, except I get a comboBox in column zero I really don't want.

    Thanks,

    Dave Thomas
    Last edited by davethomaspilot; 9th January 2013 at 16:01. Reason: Code snippet for adding combobox items isn't the slot, but another method

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Must all cellWidgets in a QTable be the same widget type?

    Cell widgets can be of any type. I suggest you check the return value of dynamic_cast before trying to dereference the received pointer.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Must all cellWidgets in a QTable be the same widget type?

    The pointer returned from the dynamic_cast is not Null.

    Here's what it looks like:

    watch.jpg
    Also, note the line:


    qb->clear();

    Doesn't raise and excpetion

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Must all cellWidgets in a QTable be the same widget type?

    If it's not null then there is a combobox there. Where does the code crash? Show us the backtrace.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Must all cellWidgets in a QTable be the same widget type?

    It looks like the crash is occuring in a slot that got called as a result of an item change in the QComboBox. Here's my method for the slot:

    Qt Code:
    1. void CollectStats::check_lineup(int n)
    2. {
    3. QComboBox *changed_cell= dynamic_cast<QComboBox *>(sender());
    4. changed_cell->setStyleSheet("QComboBox { background-color: white;font:16pt }");
    5. for (int i=0;i<6;i++)
    6. {
    7.  
    8. QComboBox *qp = dynamic_cast<QComboBox *>(runOrder->cellWidget(0,i));
    9. if (sender() ==qp) continue;
    10. if (qp->currentIndex()==n)
    11. qp->setStyleSheet("QComboBox { background-color: red; font:16pt }");
    12. else
    13. qp->setStyleSheet("QComboBox { background-color: white; font:16pt }");
    14.  
    15. }
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 

    It looks like it's dying on the

    Qt Code:
    1. if (qp->currentIndex()==n)
    To copy to clipboard, switch view to plain text mode 

    I'm not sure how to print the stack trace from VS, so here are a couple of screen shots:
    stack2.pngstack1.jpg

    Thanks for the attention!

    Found it,

    Should be

    for (int i=1;i<7;i++)

    In the check_lineup method, after I eliminated the combobox in column 0.

    I feel stupid, thanks for your extremely fast replies--it got me pointed in the right direction!

    Dave Thomas

Similar Threads

  1. how to get complete row from qtable widget?
    By aurora in forum Qt Programming
    Replies: 2
    Last Post: 10th January 2012, 10:20
  2. QTable Widget
    By kavinsiva in forum Newbie
    Replies: 5
    Last Post: 23rd October 2009, 13:50
  3. problem with QTable Widget
    By navi1084 in forum Qt Programming
    Replies: 1
    Last Post: 23rd September 2008, 06:09
  4. QTable Widget
    By csvivek in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2008, 13:37

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.