Results 1 to 7 of 7

Thread: QTableWidget (resizing rows, turning off selection, etc.)

  1. #1
    Join Date
    Jan 2007
    Posts
    29
    Thanks
    3

    Default QTableWidget (resizing rows, turning off selection, etc.)

    Using 4.2, and have some issues w/ a QTableWidget.

    First, and foremost, I'm trying to make cells completely unselectable and unfocus-able. I thought this would work for, but will not. Even w/ this below, all cells are both focus & select-friendly.

    Qt Code:
    1. QTableWidget *tbl = new QTableWidget( 0, 8, this );
    2. tbl->setSelectionMode( QAbstractItemView::NoSelection );
    3. tbl->setFocusPolicy( Qt::NoFocus );
    To copy to clipboard, switch view to plain text mode 

    Also, I have a pretty simple piece of code (I thought). Read in a line, make a new row (at row 0 -- at the top of the widget), change the height of that row. I posted this on the QT Interest ML, and didn't get much help. AFAIK, there's no weird indexing. setRowHeight(x) should change the height of row x, no matter when it was added to the QTableWidget.

    Qt Code:
    1. void mywidget::read_line()
    2. {
    3. char* cfg_file = getenv("FILE");
    4. QFile file( cfg_file );
    5.  
    6. if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) ) {
    7. while ( !file.atEnd() ) {
    8. QByteArray line = file.readLine();
    9. QStringList vals = QStringList::split( ',', line, 0 );
    10. if ( vals.size() == no_of_cols ) { // valid line of text
    11. tbl_trades->insertRow( 0 ); // new row
    12. tbl_trades->setRowHeight( 0, 20 ); // resize new row
    13. for ( int i = 0; i < no_of_cols; i++ ) { //
    14. QTableWidgetItem *temp = new QTableWidgetItem( vals[i] );
    15. tbl_trades->setItem( 0, i, temp );
    16. }
    17. }
    18. }
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableWidget (resizing rows, turning off selection, etc.)

    As to your first question, add the line:
    Qt Code:
    1. tbl->setEditTriggers(QAbstractItemView::NoEditTriggers);
    To copy to clipboard, switch view to plain text mode 

    And if you want all rows to have equal size you should probably implement a delegate. ie.:
    Qt Code:
    1. class ItemDelegate : public QItemDelegate
    2. {
    3. virtual QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
    4. {
    5. return QItemDelegate::sizeHint(option, index).expandedTo(QSize(0,20));
    6. }
    7. };
    8.  
    9. ...
    10. tbl->setItemDelegate(new ItemDelegate);
    11. ...
    12. tbl->resizeRowsToContents();
    13. ...
    To copy to clipboard, switch view to plain text mode 

    or reimplement QTableView::sizeHintForRow(int row) to always return the desired height.
    Last edited by spud; 8th January 2007 at 10:14.

  3. #3
    Join Date
    Jan 2007
    Posts
    29
    Thanks
    3

    Default Re: QTableWidget (resizing rows, turning off selection, etc.)

    Quote Originally Posted by spud View Post
    As to your first question, add the line:
    Qt Code:
    1. tbl->setEditTriggers(QAbstractItemView::NoEditTriggers);
    To copy to clipboard, switch view to plain text mode 
    I was actually just looking into this. I successfully reimplemented setSelection and setFocus. Realized one last thing was missing.

    or reimplement QTableView::sizeHintForRow(int row) to always return the desired height.
    This is most likely better. This is an ongoing action of adding rows... Might as well just make them a default size. I'm curious though... some rows I will add and make them invisible if they meet a certain criteria. I don't see how that could work and my sample above setting the height to 20 would.

    EDIT: Tried this, but no luck:
    Qt Code:
    1. int QTableView::sizeHintForRow(int row) const
    2. {
    3. return 20;
    4. }
    To copy to clipboard, switch view to plain text mode 

    Is that definitely called when new rows are made?
    Last edited by kiss-o-matic; 8th January 2007 at 11:54.

  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 (resizing rows, turning off selection, etc.)

    Quote Originally Posted by kiss-o-matic View Post
    EDIT: Tried this, but no luck:
    Qt Code:
    1. int QTableView::sizeHintForRow(int row) const
    2. {
    3. return 20;
    4. }
    To copy to clipboard, switch view to plain text mode 

    Is that definitely called when new rows are made?
    Where is that function defined? This is not how you override a function in C++. It should be more like:
    Qt Code:
    1. class MyTableView : public QTableView
    2. {
    3. ...
    4. protected:
    5. int sizeHintForRow(int row) const;
    6. ...
    7. };
    8.  
    9. int MyTableView::sizeHintForRow(int row) const
    10. {
    11. return something;
    12. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. #5
    Join Date
    Jan 2007
    Posts
    29
    Thanks
    3

    Default Re: QTableWidget (resizing rows, turning off selection, etc.)

    I tried your code verbatim, and still got no action. Tried to output something to cout as well, denoting that it was inside the overridden function, but got nothing. My guess is that it's not being called.

  6. #6
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Post Re: QTableWidget (resizing rows, turning off selection, etc.)

    Here is a standalone example. let me know if it works for you.

    Qt Code:
    1. #include <QApplication>
    2. #include <QTableWidget>
    3.  
    4. class MyTableWidget : public QTableWidget
    5. {
    6. public:
    7. MyTableWidget (int r, int c, QWidget *parent = 0)
    8. : QTableWidget(r,c,parent)
    9. {
    10. }
    11. protected:
    12. int sizeHintForRow(int row) const
    13. {
    14. return 100;
    15. }
    16. };
    17.  
    18. int main(int argc, char* argv[])
    19. {
    20. QApplication app(argc, argv);
    21. MyTableWidget *tbl = new MyTableWidget( 10, 8 );
    22. tbl->setSelectionMode( QAbstractItemView::NoSelection );
    23. tbl->setEditTriggers(QAbstractItemView::NoEditTriggers);
    24. tbl->setFocusPolicy( Qt::NoFocus );
    25.  
    26. for ( int r = 0; r < 10; r++)
    27. for ( int c = 0; c < 8; c++ )
    28. tbl->setItem( r, c, new QTableWidgetItem( QString::number(r+1)+" "+QString::number(c+1)) );
    29.  
    30. tbl->resizeRowsToContents();
    31. tbl->show();
    32. return app.exec();
    33. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2007
    Posts
    29
    Thanks
    3

    Default Re: QTableWidget (resizing rows, turning off selection, etc.)

    Well, I'm never resizing the rows... only making them. That's the first major difference I see between my code and yours. As before, I will continually be adding rows (maybe up to 1000). Running a routine that resizes them all doesn't seem efficient in the least.

Similar Threads

  1. QTableWidget Sorting Multiple Selection
    By rhiacasta in forum Qt Programming
    Replies: 1
    Last Post: 30th August 2006, 21:05
  2. 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.