Results 1 to 18 of 18

Thread: QTableWidget Problem, setRowWidget? :P

  1. #1
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QTableWidget Problem, setRowWidget? :P

    Ok, well I want to create a sort of list such as this:

    [information1] [otherinfo1] [somemoreinfo1] [button1] [othbutton1]
    [information2] [otherinfo2] [somemoreinfo2] [button2] [othbutton2]

    So I made a TableWidget, because it has columns and you can add Widgets inside a cell.
    But let's say I am making a list of 30 of these items. I would use some QList<QTableWidgetItem *>, and loop through these. Seems all perfect, of course, until someone clicks on a header to sort it. I mean I want to sort them, but I don't want each column to be separate from their row.

    In other words, if someone presses sort by otherinfo... then let's say otherinfo3 comes first? well then the cell next to it should be information3 and somemoreinfo3... Do you see what I mean?

    How would I fix that?

  2. #2
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableWidget Problem, setRowWidget? :P

    You must use QTableView methods to be able to fix this

    With QTableView, you can access to the header widgets : see QTableView::verticalHeader() / horizontalHeader().

    These headers emit useful signals you can connect to : for example, sectionClicked(int).

    You could a slot to this signal, that would sort all your other columns, according to the one that was clicked !

    So it would be something like this :
    Qt Code:
    1. // Signals Init
    2.  
    3. void Class::initSignals()
    4. {
    5. connect (tablewidget->horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(sortAllColumns(int)) );
    6. }
    7.  
    8. void Class::sortAllColumns(int)
    9. {
    10. // Code for sorting ! :)
    11. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to guilugi for this useful post:

    VireX (5th April 2007)

  4. #3
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget Problem, setRowWidget? :P

    I don't believe I have to do that.
    QtableWidget has all the TableView functions.
    Plus, I need setCellWidget in QTableWidget...

    Any ideas on how I could sort them all together, so they won't separate from each other. Like Button1 and information1 won't separate when sorted ? Anyone have an example?

  5. #4
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableWidget Problem, setRowWidget? :P

    Sure sure...I didn't want you to use a QTableview object, you can of course access those methods through QTableWidget

  6. #5
    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: QTableWidget Problem, setRowWidget? :P

    Could you define "separate"?

  7. #6
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableWidget Problem, setRowWidget? :P

    I think he wants to sort the whole tablewidget...in accordance to the column that has been clicked (and sorted)

  8. #7
    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: QTableWidget Problem, setRowWidget? :P

    Ok, but what is the difference between "whole tablewidget" and the effect he currently experiences?

  9. #8
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget Problem, setRowWidget? :P

    Well If I were to sort a column, it would sort ONLY that column, not ALL columns, so
    If i sort Information1:

    Information 1 AND [otherinfo1] [somemoreinfo1] [button1] [othbutton1]
    will be in separate rows... So what am I suppose to do?

    I'm not sure how I would override the column sorting like that.

  10. #9
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget Problem, setRowWidget? :P

    Did you guys understand my problem?

    In other words, each column item in each row needs to LOCK onto the columns next to it.

    Info | Button
    1)[info1][button1]
    2)[info2][button2]
    3)[info3][button3]

    Then someone clicks Button header item. Then suddenly, it becomes:

    Info | Button
    1)[info1][button3]
    2)[info2][button2]
    3)[info3][button1]


    But this will make button3 and info1 next to each other, which is incorrect.

    I want it so that when someone clicks "Button" header item.
    It sorts like this:

    Info | Button
    1)[info3][button3]
    2)[info2][button2]
    3)[info1][button1]

    So in other words the numbers need to be locked together. Is this any clearer?

  11. #10
    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: QTableWidget Problem, setRowWidget? :P

    Did you write the sorting code yourself? The table view sorts rows by default, not columns. Maybe the effect is caused by the fact, that you use cell widgets? In that case you'll probably have to move the widgets to appropriate cells yourself.

  12. The following user says thank you to wysota for this useful post:

    VireX (6th April 2007)

  13. #11
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableWidget Problem, setRowWidget? :P

    Yes, I understand your problem :

    - You click on a column header to sort data, and you also want the other columns to be adjusted, in order to keep your rows the same !

    - If you really want to keep QTableWidget, I think you should :

    1) deactivate the default sorting method with setSortingEnabled(false)
    2) connect a custom sorting slot to the header signal sectionClicked()
    3) implement your custom sorting function to sort just like you want

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

    VireX (6th April 2007)

  15. #12
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget Problem, setRowWidget? :P

    Thanks for the help guys. However, I tried a small test with random text in the cells, and it turns out wysota is right, it is automatically locked... I am so shocked, I tested it before and I coulda sworn it wasn't like that! I cannot believe it... But I definitely belong to this forum for another month now .

  16. #13
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget Problem, setRowWidget? :P

    Qt Code:
    1. for(int i = 1; i < 53; i++){
    2. t_Items << new QTableWidgetItem(QString("%1").arg(i));
    3. p_Items << new QTableWidgetItem(QString("55 - %1").arg(i));
    4. s_Items << new QTableWidgetItem(QString("milliseconds: %1 ms").arg(i));
    5. i_Items << new QTableWidgetItem(QString("Description %1").arg(i));
    6. TableW->setItem(i-1, 0, t_Items[i-1]);
    7. TableW->setItem(i-1, 1, p_Items[i-1]);
    8. TableW->setItem(i-1, 3, s_Items[i-1]);
    9. TableW->setItem(i-1, 4, i_Items[i-1]);
    10. }
    To copy to clipboard, switch view to plain text mode 
    So I am trying to use a loop, I am adding some information onto the QTableWidget.
    There are already 55 rows available, and I'm adding them onto the table, but for some reason, the program usually crashes.

    all the t_Items and other Items are QList<QTableWidgetItem *>

  17. #14
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableWidget Problem, setRowWidget? :P

    You should post a larger sample of code, a compilable one would be the best.

    Also, post the debug backtrace of your crash !

  18. #15
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget Problem, setRowWidget? :P

    I cannot debug. When I debug, there is no problem except that
    setItem for column 2-4-5 don't work except for the VERY LAST row (52)

    My logs say, that it stops recording in the game loop (counts to 52 and program crashes) when NOT DEBUGGING;;;; when debugging... absolutely no problems.

  19. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget Problem, setRowWidget? :P

    Quote Originally Posted by VireX View Post
    There are already 55 rows available, and I'm adding them onto the table, but for some reason, the program usually crashes.
    Maybe there isn't enough columns?

  20. #17
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget Problem, setRowWidget? :P

    You know what nvm, you guys don't see anything wrong with it.

    Also, the fact that me commenting out CreateList(), still causes a crash (but this time moves to end of the program), but when theres CreateList() (with almost no code except a log function, which proves its something related to my program and not Qt Table).

    Ok one last question
    Let's say I have 2 pushbuttons:
    label: Add
    Labal: Del

    If I were to setCellWidget the pushbutton to one column, but if I were to click the header "to sort", would it sort all the Add buttons at top and Del buttons at bottom (or vice-versa)? or would it not know how to sort PushButtons?

  21. #18
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget Problem, setRowWidget? :P

    Is there any sample sorting code, to sort some widgets or something?
    Last edited by VireX; 6th April 2007 at 18:20.

Similar Threads

  1. QTableWidget issues
    By Djony in forum Qt Programming
    Replies: 42
    Last Post: 19th December 2006, 23:27
  2. QTableWidget editing question
    By Trasmeister in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2006, 18:46
  3. Problem inserting in QTableWidget
    By DPinLV in forum Qt Programming
    Replies: 2
    Last Post: 2nd August 2006, 00:10
  4. Replies: 16
    Last Post: 7th March 2006, 15:57
  5. Replies: 6
    Last Post: 5th March 2006, 21: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.