PDA

View Full Version : QTableWidget (resizing rows, turning off selection, etc.)



kiss-o-matic
8th January 2007, 06:36
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.



QTableWidget *tbl = new QTableWidget( 0, 8, this );
tbl->setSelectionMode( QAbstractItemView::NoSelection );
tbl->setFocusPolicy( Qt::NoFocus );


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.



void mywidget::read_line()
{
char* cfg_file = getenv("FILE");
QFile file( cfg_file );

if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) ) {
while ( !file.atEnd() ) {
QByteArray line = file.readLine();
QStringList vals = QStringList::split( ',', line, 0 );
if ( vals.size() == no_of_cols ) { // valid line of text
tbl_trades->insertRow( 0 ); // new row
tbl_trades->setRowHeight( 0, 20 ); // resize new row
for ( int i = 0; i < no_of_cols; i++ ) { //
QTableWidgetItem *temp = new QTableWidgetItem( vals[i] );
tbl_trades->setItem( 0, i, temp );
}
}
}
}
}

spud
8th January 2007, 11:04
As to your first question, add the line:

tbl->setEditTriggers(QAbstractItemView::NoEditTriggers) ;

And if you want all rows to have equal size you should probably implement a delegate. ie.:


class ItemDelegate : public QItemDelegate
{
virtual QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
return QItemDelegate::sizeHint(option, index).expandedTo(QSize(0,20));
}
};

...
tbl->setItemDelegate(new ItemDelegate);
...
tbl->resizeRowsToContents();
...


or reimplement QTableView::sizeHintForRow(int row) to always return the desired height.

kiss-o-matic
8th January 2007, 12:49
As to your first question, add the line:

tbl->setEditTriggers(QAbstractItemView::NoEditTriggers) ;

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:


int QTableView::sizeHintForRow(int row) const
{
return 20;
}


Is that definitely called when new rows are made?

jpn
8th January 2007, 13:11
EDIT: Tried this, but no luck:


int QTableView::sizeHintForRow(int row) const
{
return 20;
}


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:


class MyTableView : public QTableView
{
...
protected:
int sizeHintForRow(int row) const;
...
};

int MyTableView::sizeHintForRow(int row) const
{
return something;
}

kiss-o-matic
9th January 2007, 08:40
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.

spud
9th January 2007, 16:05
Here is a standalone example. let me know if it works for you.



#include <QApplication>
#include <QTableWidget>

class MyTableWidget : public QTableWidget
{
public:
MyTableWidget (int r, int c, QWidget *parent = 0)
: QTableWidget(r,c,parent)
{
}
protected:
int sizeHintForRow(int row) const
{
return 100;
}
};

int main(int argc, char* argv[])
{
QApplication app(argc, argv);
MyTableWidget *tbl = new MyTableWidget( 10, 8 );
tbl->setSelectionMode( QAbstractItemView::NoSelection );
tbl->setEditTriggers(QAbstractItemView::NoEditTriggers) ;
tbl->setFocusPolicy( Qt::NoFocus );

for ( int r = 0; r < 10; r++)
for ( int c = 0; c < 8; c++ )
tbl->setItem( r, c, new QTableWidgetItem( QString::number(r+1)+" "+QString::number(c+1)) );

tbl->resizeRowsToContents();
tbl->show();
return app.exec();
}

kiss-o-matic
11th January 2007, 02:57
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.