Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Different widths for each column in different row in QTableWidget
QtWS25 Last Chance

Different widths for each column in different row in QTableWidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt5
5 Posts 4 Posters 2.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kalyan
    wrote on last edited by A Former User
    #1

    Hi,

    I am creating a QTableWidget with multiple rows and columns.
    And inserting custom QWidgets into the table.
    These custom widgets are having diff. widths.

    I am giving the below settings for the QTableWidget:

    QTableWidget *m_pTimelineTable = new QTableWidget();
    
    m_pTimelineTable->setRowCount(m_masterLayout.sRowCount);
    m_pTimelineTable->setColumnCount(m_masterLayout.sColCount);
    
    m_pTimelineTable->horizontalHeader()->hide();
    m_pTimelineTable->verticalHeader()->hide();
    
    m_pTimelineTable->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContent s);
    m_pTimelineTable->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContent s);
    
    m_pTimelineTable->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysO ff);
    m_pTimelineTable->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff );
    ...
    ...
    //Custom Widget
    MyView *mView = new MyView(); //Inherited from QWidget
    int iCustomWidth = 600;
    mView->setGeometry(0,0,iCustomWidth,250);
    
    //This iCustomWidth will be different for each custom widget
    ...
    ...
    m_pTimelineTable->setCellWidget((*bIterator).row_num, (*bIterator).col_num, mView);[/I][/I]
    

    Now, all CustomWidgets in each cell of the QTableWidget is having the same width.

    Anyone, could you please suggest me to get the diff. widths of custom widgets in each cell ...

    Thanks
    Kalyan

    jsulmJ 1 Reply Last reply
    0
    • K Kalyan

      Hi,

      I am creating a QTableWidget with multiple rows and columns.
      And inserting custom QWidgets into the table.
      These custom widgets are having diff. widths.

      I am giving the below settings for the QTableWidget:

      QTableWidget *m_pTimelineTable = new QTableWidget();
      
      m_pTimelineTable->setRowCount(m_masterLayout.sRowCount);
      m_pTimelineTable->setColumnCount(m_masterLayout.sColCount);
      
      m_pTimelineTable->horizontalHeader()->hide();
      m_pTimelineTable->verticalHeader()->hide();
      
      m_pTimelineTable->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContent s);
      m_pTimelineTable->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContent s);
      
      m_pTimelineTable->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysO ff);
      m_pTimelineTable->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff );
      ...
      ...
      //Custom Widget
      MyView *mView = new MyView(); //Inherited from QWidget
      int iCustomWidth = 600;
      mView->setGeometry(0,0,iCustomWidth,250);
      
      //This iCustomWidth will be different for each custom widget
      ...
      ...
      m_pTimelineTable->setCellWidget((*bIterator).row_num, (*bIterator).col_num, mView);[/I][/I]
      

      Now, all CustomWidgets in each cell of the QTableWidget is having the same width.

      Anyone, could you please suggest me to get the diff. widths of custom widgets in each cell ...

      Thanks
      Kalyan

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Kalyan "Different widths for each column in different row" - do you mean you want the same column to have different width in different rows? Like:

      |    |     |      |
      ----------------------
      |  |     |           |
      ----------------------
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      K 1 Reply Last reply
      3
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3

        You can't really achieve this with QTableWidget, the closest you can come to is not add the custom widget directly but put it in another QWidget with a layout with spacers all around like:

        #include <QWidget>
        #include <QGridLayout>
        class TableContainerWidget : public QWidget{
            Q_OBJECT
            Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged)
            Q_PROPERTY(QWidget* widget READ widget WRITE setWidget NOTIFY widgetChanged)
            Q_DISABLE_COPY(TableContainerWidget)
            
        public:
            explicit TableContainerWidget(QWidget* parent=Q_NULLPTR)
                :QWidget(parent)
                ,m_alignment(Qt::AlignCenter)
            {
                m_mainLay=new QGridLayout(this);
                m_leftSpacer=new QSpacerItem(0,0,QSizePolicy::Expanding,QSizePolicy::Preferred);
                m_rightSpacer=new QSpacerItem(0,0,QSizePolicy::Expanding,QSizePolicy::Preferred);
                m_topSpacer=new QSpacerItem(0,0,QSizePolicy::Preferred,QSizePolicy::Expanding);
                m_bottomSpacer=new QSpacerItem(0,0,QSizePolicy::Preferred,QSizePolicy::Expanding);
            }
            virtual ~TableContainerWidget(){
                m_mainLay->removeItem(m_topSpacer);
                m_mainLay->removeItem(m_bottomSpacer);
                m_mainLay->removeItem(m_leftSpacer);
                m_mainLay->removeItem(m_rightSpacer);
                delete m_topSpacer;
                delete m_bottomSpacer;
                delete m_leftSpacer;
                delete m_rightSpacer;
            }
        
            Qt::Alignment alignment() const
            {
                return m_alignment;
            }
            void setAlignment(const Qt::Alignment &alignment)
            {
                if(m_alignment != alignment){
                    m_alignment = alignment;
                    m_mainLay->removeItem(m_topSpacer);
                    m_mainLay->removeItem(m_bottomSpacer);
                    m_mainLay->removeItem(m_leftSpacer);
                    m_mainLay->removeItem(m_rightSpacer);
                    if(!((m_alignment & Qt::AlignLeft) ||  (m_alignment & Qt::AlignJustify)))
                        m_mainLay->addItem(m_leftSpacer,0,0,3);
                    if(!((m_alignment & Qt::AlignRight) ||  (m_alignment & Qt::AlignJustify)))
                        m_mainLay->addItem(m_rightSpacer,0,2,3);
                    if(!(m_alignment & Qt::AlignTop))
                        m_mainLay->addItem(m_topSpacer,0,0,1,3);
                    if(!(m_alignment & Qt::AlignBottom))
                        m_mainLay->addItem(m_bottomSpacer,2,0,1,3);
                    emit alignmentChanged(m_alignment);
                }
            }
            QWidget *widget() const
            {
                return m_widget;
            }
            void setWidget(QWidget *widget)
            {
                if(m_widget==widget)
                    return;
                if(m_widget)
                    m_widget->deleteLater();
                m_widget = widget;
                if(m_widget)
                    m_mainLay->addWidget(m_widget,1,1);
                emit widgetChanged(m_widget);
            }
        signals:
            void alignmentChanged(Qt::Alignment align);
            void widgetChanged(QWidget *widget);
        private:
            QGridLayout* m_mainLay;
            QSpacerItem* m_topSpacer;
            QSpacerItem* m_bottomSpacer;
            QSpacerItem* m_leftSpacer;
            QSpacerItem* m_rightSpacer;
            Qt::Alignment m_alignment;
            QWidget* m_widget;
        };
        

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        1
        • jsulmJ jsulm

          @Kalyan "Different widths for each column in different row" - do you mean you want the same column to have different width in different rows? Like:

          |    |     |      |
          ----------------------
          |  |     |           |
          ----------------------
          
          K Offline
          K Offline
          Kalyan
          wrote on last edited by
          #4

          @jsulm Yes...exactly...the same way you shown.

          1 Reply Last reply
          0
          • hskoglundH Offline
            hskoglundH Offline
            hskoglund
            wrote on last edited by hskoglund
            #5

            Hi, if you're not having that many different rows/layouts, say 3: flavor A, flavor B, flavor C, flavor A again, you could do it using spanned columns http://doc.qt.io/qt-5/qtableview.html#setSpan

            For example, in one of my healthcare/insurance apps, I use that to implement 2 different width schemas in my QTableWidget, screen shot here: alt text

            1 Reply Last reply
            3

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved