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.
Code:
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.
Code:
void mywidget::read_line()
{
char* cfg_file = getenv("FILE");
while ( !file.atEnd() ) {
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++ ) { //
tbl_trades->setItem( 0, i, temp );
}
}
}
}
}
Re: QTableWidget (resizing rows, turning off selection, etc.)
As to your first question, add the line:
And if you want all rows to have equal size you should probably implement a delegate. ie.:
Code:
{
{
}
};
...
tbl->setItemDelegate(new ItemDelegate);
...
tbl->resizeRowsToContents();
...
or reimplement QTableView::sizeHintForRow(int row) to always return the desired height.
Re: QTableWidget (resizing rows, turning off selection, etc.)
Quote:
Originally Posted by
spud
As to your first question, add the line:
I was actually just looking into this. I successfully reimplemented setSelection and setFocus. Realized one last thing was missing.
Quote:
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:
Is that definitely called when new rows are made?
Re: QTableWidget (resizing rows, turning off selection, etc.)
Quote:
Originally Posted by
kiss-o-matic
EDIT: Tried this, but no luck:
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:
Code:
{
...
protected:
int sizeHintForRow(int row) const;
...
};
int MyTableView::sizeHintForRow(int row) const
{
return something;
}
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.
Re: QTableWidget (resizing rows, turning off selection, etc.)
Here is a standalone example. let me know if it works for you.
Code:
#include <QApplication>
#include <QTableWidget>
{
public:
MyTableWidget
(int r,
int c,
QWidget *parent
= 0) {
}
protected:
int sizeHintForRow(int row) const
{
return 100;
}
};
int main(int argc, char* argv[])
{
MyTableWidget *tbl = new MyTableWidget( 10, 8 );
tbl->setFocusPolicy( Qt::NoFocus );
for ( int r = 0; r < 10; r++)
for ( int c = 0; c < 8; c++ )
tbl->resizeRowsToContents();
tbl->show();
return app.exec();
}
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.