Results 1 to 17 of 17

Thread: inserting QCheckBox into the Header

  1. #1
    Join Date
    Mar 2008
    Posts
    25
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default inserting QCheckBox into the Header

    Hello everybody
    I have the task to insert the QCheckBox item into the vertical header.
    I looked through the demos (itemview) where picture (the star) is inserted into the vertical header by reimplementing
    method QVariant QAbstractItemModel::headerData ( int section, Qt::Orientation orientation, int role = Qt:: DisplayRole ) const [virtual]
    Qt Code:
    1. QVariant Model::headerData(int section, Qt::Orientation orientation, int role) const
    2. {
    3. static QIcon services(QPixmap(":/images/services.png"));
    4. if (role == Qt::DisplayRole)
    5. return QString::number(section);
    6. if (role == Qt::DecorationRole)
    7. return qVariantFromValue(services);
    8. return QStandardItemModel::headerData(section, orientation, role);
    9. }
    To copy to clipboard, switch view to plain text mode 
    Then I try to reload this method for my own model (CustomModel)

    Qt Code:
    1. QVariant CustomModel::headerData(int section, Qt::Orientation orientation, int role) const
    2. {
    3. if (role == Qt::DisplayRole)
    4. return QString::number(section);
    5. QObject *object = new QCheckBox;
    6. if (role == Qt::UserRole)
    7. return qVariantFromValue(object);
    8. return QStandardItemModel::headerData(section, orientation, role);
    9. }
    To copy to clipboard, switch view to plain text mode 

    but there is no effect.
    Help me please.
    Last edited by ru_core; 12th April 2008 at 20:30.

  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: inserting QCheckBox into the Header

    There will be no effect. QHeaderView is not meant to hold widgets. You need to subclass the header and put it (and handle it) there by yourself. You can use event filters to get away from subclassing but you still have to do things manually.

  3. #3
    Join Date
    Mar 2008
    Posts
    25
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: inserting QCheckBox into the Header

    thank you,
    can I do it using the
    void QTableView::setVerticalHeader ( QHeaderView * header )
    method or which way?

  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: inserting QCheckBox into the Header

    If you subclass the header class then yes, you'll have to use setVerticalHeader() to set the subclassed header.

  5. #5
    Join Date
    Mar 2008
    Posts
    25
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: inserting QCheckBox into the Header

    Quote Originally Posted by wysota View Post
    There will be no effect. QHeaderView is not meant to hold widgets. You need to subclass the header and put it (and handle it) there by yourself. You can use event filters to get away from subclassing but you still have to do things manually.
    so, if I got correct: I need to create widget with the QCheckBox in class customClass (subclassed from QHeaderView) and simply put it by setVerticalHeader() into the view? Correct?

    I am new to Qt

  6. #6
    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: inserting QCheckBox into the Header

    Yes, that's correct. And you need to make sure it changes position when sections of the header are resized, etc. It's quite a bit of work, so before you start doing that think if you really need it. Maybe you could just use checkboxes in items themselves, that's much easier to achieve.

  7. #7
    Join Date
    Mar 2008
    Posts
    25
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: inserting QCheckBox into the Header

    Quote Originally Posted by wysota View Post
    Yes, that's correct. And you need to make sure it changes position when sections of the header are resized, etc. It's quite a bit of work, so before you start doing that think if you really need it. Maybe you could just use checkboxes in items themselves, that's much easier to achieve.
    thank you again for the reply.
    at this stage I got confused. So you propose subclass QStandardItem class and just create there checkbox. Then I have and put new object to model by
    void QStandardItemModel::setVerticalHeaderItem ( int row, QStandardItem * item )
    ?

  8. #8
    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: inserting QCheckBox into the Header

    I didn't say anything about QStandardItem. If you try to explain what do you need the checkbox for, maybe we'll find a solution that doesn't require writing as much code as subclassing the header does.

  9. #9
    Join Date
    Mar 2008
    Posts
    25
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: inserting QCheckBox into the Header

    Quote Originally Posted by wysota View Post
    I didn't say anything about QStandardItem. If you try to explain what do you need the checkbox for, maybe we'll find a solution that doesn't require writing as much code as subclassing the header does.
    There is a QTableView object with model. I need to create the functionality to delele some row(s) from model.
    So I decided to use the checboxes to indicate which rows have to be deleted. Then by pushing button user can delete the checked rows.

    Could it be done other way?

  10. #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: inserting QCheckBox into the Header

    Why not simply select the rows that are to be deleted using regular selection capabilities?

  11. #11
    Join Date
    Mar 2008
    Posts
    25
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: inserting QCheckBox into the Header

    selectig - using "shift" key and then delete'em some way?

  12. #12
    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: inserting QCheckBox into the Header

    Yes, shift and control.

  13. #13
    Join Date
    Mar 2008
    Posts
    25
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: inserting QCheckBox into the Header

    this program is not for every day usage, just like a calculator for students - to calculate some values. Whould it be good idea to propose standard "select-delete" capabilities instead of checkboxes?

  14. #14
    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: inserting QCheckBox into the Header

    I don't understand what you are asking about... A regular way to select items is to use selection... So you can safely use selection for selecting

  15. #15
    Join Date
    Mar 2008
    Posts
    25
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: inserting QCheckBox into the Header

    ok, it seems I subtilize to much I will follow your sugestion to use standard selection for selecting
    thank you very much for the help.

  16. #16
    Join Date
    Dec 2007
    Location
    Austin, TX
    Posts
    43
    Thanks
    12
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: inserting QCheckBox into the Header

    If you're using it to delete rows, why not just make a column with the QTableWidgetItem having the flag Qt::ItemIsUserCheckable?

    Or if you want to delete a row at a time, connect the table's (list's) clicked() signal to a delete function? Or even have a column with a "delete" label/button/icon & only have the delete function work if the column is correct?

    Vycke

  17. #17
    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: inserting QCheckBox into the Header

    Quote Originally Posted by vycke View Post
    If you're using it to delete rows, why not just make a column with the QTableWidgetItem having the flag Qt::ItemIsUserCheckable?
    Because that's an awful hack that comes from the world wide web where html doesn't allow you to perform real selections.

Similar Threads

  1. How to edit Horizontal Header Item in QTableWidget
    By ioannis in forum Qt Programming
    Replies: 6
    Last Post: 5th March 2013, 19:50
  2. Replies: 0
    Last Post: 10th November 2006, 14:46

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.