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

Adding tableview to scene, appearing small and with scroll bars



  • Hello,
    I have added a QTableView widget to a scene associated with a graphics view. But The tableview is appearing very small with scroll bars enabled in the view. I Do know that i need to do some coordinate conversions. Im a newbie to qt and can someone help me what conversions needs to be made . Thanks for any help in advance.
    Screenshot 2021-05-30 at 10.25.43 PM.png


  • Lifetime Qt Champion

    Hi
    Im not sure what it uses for default size (size hit function, i guess) but you can just
    call resize() before calling scene->addWidget()

    alt text

    auto m_pTableWidget = new QTableWidget();
        m_pTableWidget->setRowCount(10);
        m_pTableWidget->setColumnCount(3);
        m_pTableWidget->setHorizontalHeaderLabels(QStringList() << "#" << "Name" << "Text");   
        m_pTableWidget->setItem(0, 1, new QTableWidgetItem("Hello"));
        m_pTableWidget->resize(400,400);
    
        scene->addWidget( m_pTableWidget);
    
    


  • @mrjj said in Adding tableview to scene, appearing small and with scroll bars:

    400,400

    Thanks for the quick reply @mrjj . Im using a qtableview with a model. so the data is inserted dynamically .. i wont be able to hardcode the size in resize function right.
    The issue can be reproducible with the below code in a mainwindow application.

        ui->setupUi(this);
    
        QStandardItemModel *indexPageModel=new QStandardItemModel;
        QTableView *indexPageTableView=new QTableView();
        indexPageTableView->setModel(indexPageModel);
    
    //row1
        QStandardItem *item00 = new QStandardItem("   Title  ");
        item00->setTextAlignment(Qt::AlignCenter);
        indexPageModel->setItem(0, 0, item00);
    
        QStandardItem *item01 = new QStandardItem("  X-axis  ");
        item01->setTextAlignment(Qt::AlignCenter);
        indexPageModel->setItem(0, 1, item01);
    
        QStandardItem *item02 = new QStandardItem("  Y-axis  ");
        item02->setTextAlignment(Qt::AlignCenter);
        indexPageModel->setItem(0, 2, item02);
    
        QStandardItem *item03 = new QStandardItem("    Page#    ");
        item03->setTextAlignment(Qt::AlignCenter);
        indexPageModel->setItem(0, 3, item03);
    
        QStandardItem *item04 = new QStandardItem("    Trend#    ");
        item04->setTextAlignment(Qt::AlignCenter);
        indexPageModel->setItem(0, 4, item04);
    
        QStandardItem *item05 = new QStandardItem("Trend Relationship");
        item05->setTextAlignment(Qt::AlignCenter);
        indexPageModel->setItem(0, 5, item05);
    
        indexPageTableView->resizeColumnsToContents();
    
        QGraphicsScene *scene=new QGraphicsScene(this);
        QGraphicsProxyWidget *proxy = scene->addWidget(indexPageTableView);
        ui->graphicsView->setScene(scene);
        ui->graphicsView->setAlignment(Qt::AlignTop | Qt::AlignLeft);```

  • Lifetime Qt Champion

    Hi
    Normally you assign some portion of the form to the table and if its bigger, it get scrollbars.

    Is your goal to sum up all the header size so that it never will show scrollbars ?

    Anyway, you could do like

    .....
      indexPageTableView->resizeColumnsToContents();
    
       int width = (indexPageModel->columnCount() - 1) + indexPageTableView->verticalHeader()->width();    
            for (int column = 0; column < indexPageModel->columnCount(); column++)    
                width = width + indexPageTableView->columnWidth(column);    
      indexPageTableView->setMinimumWidth(width);
    

    alt text



  • This works perfect . Thanks you so much.


  • Lifetime Qt Champion

    @sachinrd
    Np. providing code I can just run always help the motivation to have a look.