QTableWidget: how to show vertical header and set its size policy
-
wrote on 11 Mar 2020, 18:18 last edited by Black Imp 3 Nov 2020, 18:19
I'm using a QTableWidget and I need to show both horizontal and vertical header.
If I simply pass a list of strings for vertical header and set it visible it doesn't show.
I've found out if I set a fixed width it finally appears.
How can I make vertical header fit its size to its caption list without specifying a fixed width?QTableWidget table; QStringList verHeaderList << "One" << "Two" << "Three"; table.setRowCount(verHeaderList.size()); table.setColumnCount(5); table.setVerticalHeaderLabels(verHeaderList); table.verticalHeader()->setVisible(true); table.verticalHeader()->setFixedWidth(180); // if I omit this I can't see vertical header
-
Your code does not compile. This works fine for me:
int main(int argc, char *argv[]) { QApplication app(argc, argv); QTableWidget table; QStringList verHeaderList({"One", "Two", "Three"}); table.setRowCount(verHeaderList.size()); table.setColumnCount(5); table.setVerticalHeaderLabels(verHeaderList); table.verticalHeader()->setVisible(true); table.show(); return app.exec(); }
-
Your code does not compile. This works fine for me:
int main(int argc, char *argv[]) { QApplication app(argc, argv); QTableWidget table; QStringList verHeaderList({"One", "Two", "Three"}); table.setRowCount(verHeaderList.size()); table.setColumnCount(5); table.setVerticalHeaderLabels(verHeaderList); table.verticalHeader()->setVisible(true); table.show(); return app.exec(); }
wrote on 11 Mar 2020, 18:48 last edited by@Christian-Ehrlicher
thank you I obviously extracted some lines from a complex code I can't report entirely.
Even creating a simple table like that, after you fixing, the problem remains: no vertical header appears. -
As I said - it works fine for me. Which OS and Qt version do you use?
-
As I said - it works fine for me. Which OS and Qt version do you use?
wrote on 11 Mar 2020, 19:05 last edited by@Christian-Ehrlicher
windows 10, qt 5.12.5 msvc 2017 64bit -
As I said - it works fine for me. Which OS and Qt version do you use?
wrote on 11 Mar 2020, 19:09 last edited by@Christian-Ehrlicher
Ok I've compiled separately this example and it works fine.
The actual one is created in a QMdiSubWindow. I'll check for some Size policy inherited -
wrote on 11 Mar 2020, 19:52 last edited by Black Imp 3 Nov 2020, 20:15
TableSubWindow::TableSubWindow(QWidget * parent, Qt::WindowFlags flags):QMdiSubWindow(parent, flags) { QSize size = UISupport::getInstance().getScreenGeometry(); setAcceptDrops(true); setWindowTitle("Untitled"); this->setContextMenuPolicy(Qt::CustomContextMenu); QWidget *mMainWidget = new QWidget(); mMainWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); QHBoxLayout * mMainLayout = new QHBoxLayout(); mMainWidget->setLayout(mMainLayout); mMainLayout->setSpacing(0); mMainLayout->setContentsMargins(0,0,0,0); setWidget(mMainWidget); QVBoxLayout * mLeftLayout = new QVBoxLayout(); mLeftLayout->setContentsMargins(0,0,0,0); mLeftLayout->setSpacing(0); QVBoxLayout * mRightLayout = new QVBoxLayout(); mRightLayout->setContentsMargins(0,0,0,0); mRightLayout->setSpacing(0); mMainLayout->addLayout(mLeftLayout); mMainLayout->addLayout(mRightLayout); mTable = new Table(); mLeftLayout->addWidget(mTable); setFocus(); setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); } Table::Table():QTableWidget(nullptr) { QStringList verHeaderList({"One", "Two", "Three"}); setRowCount(verHeaderList.size()); setColumnCount(5); setVerticalHeaderLabels(verHeaderList); verticalHeader()->setVisible(true); }
with Table being
class Table : public QTableWidget { // ... }
-
Please provide a minimal, compilable example as I did if you want to prove that it's a Qt problem.
-
Hi,
Beside what @Christian-Ehrlicher requested, are you sure that you are not modifying your table widget in another part of the code ?
I see you register it with something. That something might be changing your table widget by deleting its content.
-
Hi,
Beside what @Christian-Ehrlicher requested, are you sure that you are not modifying your table widget in another part of the code ?
I see you register it with something. That something might be changing your table widget by deleting its content.
wrote on 11 Mar 2020, 20:13 last edited by@SGaist I' m trying to extract a minimal code from a huge application which is quite hard. That ragistration has nothing to do with UI, it's simply a way for receiving async data from net which is completely detached at the moment and I forgot to remove it.
The real table I should use is created and modified dynamically by reading file or a settings dialog so I replaced it with that minimal hardcoded table which doesn't work anyway. If hardcoded table worked I would be able myself to understand how to fix the rest.
It works only if I create the table in main as suggested in his example but not if I put it inside a qmdiwindow with layouts structures easily readable from my code -
Please provide a minimal, compilable example as I did if you want to prove that it's a Qt problem.
wrote on 11 Mar 2020, 20:21 last edited by Black Imp 3 Nov 2020, 20:22@Christian-Ehrlicher maybe you miss the point: I don't waste my time trying to prove qt has some issues; if it were up to me I'd have never even used qt for the professional application I'm developing but it was not my decision and from answers like yours I've received sometimes I'm getting more and more convinced my client made the wrong decision.
I'm just trying to understand why I can't make it work as it seems it should be. By the way now it's quite clear it depends either on qmdisubwindow or on my layouts structures and settings -
@SGaist I' m trying to extract a minimal code from a huge application which is quite hard. That ragistration has nothing to do with UI, it's simply a way for receiving async data from net which is completely detached at the moment and I forgot to remove it.
The real table I should use is created and modified dynamically by reading file or a settings dialog so I replaced it with that minimal hardcoded table which doesn't work anyway. If hardcoded table worked I would be able myself to understand how to fix the rest.
It works only if I create the table in main as suggested in his example but not if I put it inside a qmdiwindow with layouts structures easily readable from my code@Black-Imp said in QTableWidget: how to show vertical header and set its size policy:
It works only if I create the table in main as suggested in his example but not if I put it inside a qmdiwindow with layouts structures easily readable from my code
In that case, please make it a complete example that we can build to check what is going on.
-
wrote on 12 Mar 2020, 04:02 last edited by
It works.
Sorry there was a huge string with style sheet in main and a row sets the style of all QHeaderView. That caused my problem.Thank you
-
@Black-Imp said in QTableWidget: how to show vertical header and set its size policy:
Sorry there was a huge string with style sheet in main and a row sets the style of all QHeaderView. That caused my problem.
That's the reason why I wanted you to create a minimal example.
Please mark the topic as solved then.
2/14