Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTableWidget: how to show vertical header and set its size policy



  • 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
    

  • Qt Champions 2019

    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();
    }
    
    


  • @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.


  • Qt Champions 2019

    As I said - it works fine for me. Which OS and Qt version do you use?



  • @Christian-Ehrlicher
    windows 10, qt 5.12.5 msvc 2017 64bit



  • @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



  • 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
    {
       // ...
    }
    

  • Qt Champions 2019

    Please provide a minimal, compilable example as I did if you want to prove that it's a Qt problem.


  • Lifetime Qt Champion

    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.



  • @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



  • @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


  • Lifetime Qt Champion

    @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.



  • 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


  • Qt Champions 2019

    @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.


Log in to reply