Different widths for each column in different row in QTableWidget
-
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 -
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 -
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; };
-
@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:
| | | | ---------------------- | | | | ----------------------
-
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: