QTableWidget: horizontal scroll bar not displayed
Solved
General and Desktop
-
Hi,
I have a QTableWidget that is filled dynamically. I have configured it so that colum size be resized on contents. So I expected to have the horizontal scroll bar when text is too long for the view. But I never have the scroll bar. I found differnet posts on Internet related to this problem (it seems to be recurrent). But none of the proposed solution works in my case. And, as in these posts, even the fact to set "always display the scroll bar" has no effect. I always have the below result:
Below my code (.ui and .cpp):
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>HelpUi</class> <widget class="QDialog" name="HelpUi"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>732</width> <height>595</height> </rect> </property> <property name="windowTitle"> <string>Dialog</string> </property> <layout class="QVBoxLayout" name="verticalLayout"> <item> <widget class="QTabWidget" name="tabWidget"> <property name="currentIndex"> <number>1</number> </property> <widget class="QWidget" name="m_labelTab"> <attribute name="title"> <string>Labeling</string> </attribute> <layout class="QVBoxLayout" name="verticalLayout_2"> <item> <widget class="QLabel" name="m_productLbl"> <property name="text"> <string>TextLabel</string> </property> <property name="alignment"> <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set> </property> <property name="wordWrap"> <bool>true</bool> </property> </widget> </item> </layout> </widget> <widget class="QWidget" name="m_commandTab"> <attribute name="title"> <string>Command summary</string> </attribute> <layout class="QHBoxLayout" name="horizontalLayout"> <item> <widget class="QTableWidget" name="m_summaryTable"> <property name="horizontalScrollBarPolicy"> <enum>Qt::ScrollBarAsNeeded</enum> </property> <property name="sizeAdjustPolicy"> <enum>QAbstractScrollArea::AdjustIgnored</enum> </property> <property name="editTriggers"> <set>QAbstractItemView::NoEditTriggers</set> </property> <property name="selectionMode"> <enum>QAbstractItemView::NoSelection</enum> </property> <property name="verticalScrollMode"> <enum>QAbstractItemView::ScrollPerPixel</enum> </property> <property name="horizontalScrollMode"> <enum>QAbstractItemView::ScrollPerPixel</enum> </property> <property name="showGrid"> <bool>false</bool> </property> <property name="wordWrap"> <bool>false</bool> </property> <property name="cornerButtonEnabled"> <bool>false</bool> </property> <attribute name="horizontalHeaderStretchLastSection"> <bool>false</bool> </attribute> <attribute name="verticalHeaderVisible"> <bool>false</bool> </attribute> <column> <property name="text"> <string>Action name</string> </property> </column> <column> <property name="text"> <string>User action</string> </property> </column> <column> <property name="text"> <string>Concerned view(s)</string> </property> </column> <column> <property name="text"> <string>Effect</string> </property> </column> </widget> </item> </layout> </widget> </widget> </item> <item> <widget class="QDialogButtonBox" name="buttonBox"> <property name="orientation"> <enum>Qt::Horizontal</enum> </property> <property name="standardButtons"> <set>QDialogButtonBox::Close</set> </property> </widget> </item> </layout> </widget> <resources/> <connections> <connection> <sender>buttonBox</sender> <signal>accepted()</signal> <receiver>HelpUi</receiver> <slot>accept()</slot> <hints> <hint type="sourcelabel"> <x>248</x> <y>254</y> </hint> <hint type="destinationlabel"> <x>157</x> <y>274</y> </hint> </hints> </connection> <connection> <sender>buttonBox</sender> <signal>rejected()</signal> <receiver>HelpUi</receiver> <slot>reject()</slot> <hints> <hint type="sourcelabel"> <x>316</x> <y>260</y> </hint> <hint type="destinationlabel"> <x>286</x> <y>274</y> </hint> </hints> </connection> <connection> <sender>buttonBox</sender> <signal>clicked(QAbstractButton*)</signal> <receiver>HelpUi</receiver> <slot>close()</slot> <hints> <hint type="sourcelabel"> <x>353</x> <y>253</y> </hint> <hint type="destinationlabel"> <x>356</x> <y>308</y> </hint> </hints> </connection> </connections> </ui>
#include "HelpWidget.h" #include <QtCore/QFile> #include <QtCore/QSettings> //--------------------------------------------------------------------------------------------------------- HelpWidget::HelpWidget(QWidget* p_parent /* = nullptr */) : QDialog(p_parent) //--------------------------------------------------------------------------------------------------------- { m_ui.setupUi(this); this->fillLabeling(); this->setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint); m_ui.m_summaryTable->hideColumn(0); // Column 0 is just to be able to manage multiple definitions. m_ui.m_summaryTable->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); } //--------------------------------------------------------------------------------------------------------- HelpWidget::~HelpWidget() //--------------------------------------------------------------------------------------------------------- {} //--------------------------------------------------------------------------------------------------------- void HelpWidget::fillLabeling() //--------------------------------------------------------------------------------------------------------- { }// fillLabeling() //--------------------------------------------------------------------------------------------------------- void HelpWidget::addEvent(const QString p_eventName, const QStringList p_eventDescription) //--------------------------------------------------------------------------------------------------------- { int rowIndex = -1; for (int i = 0; i < m_ui.m_summaryTable->rowCount(); ++i) { // Search for a row with the same event name and same view if ((m_ui.m_summaryTable->item(i, 0)->text() == p_eventName) && (p_eventDescription.size() >= 2) && (m_ui.m_summaryTable->item(i, 2)->text() == p_eventDescription[1])) { // Item found rowIndex = i; break; } } if (rowIndex >= 0) { // an item already defined. Replace the content for (int i = 0; i < p_eventDescription.size(); ++i) { m_ui.m_summaryTable->item(rowIndex, i + 1)->setText(p_eventDescription[i]); } } else { // Add a full row rowIndex = m_ui.m_summaryTable->rowCount(); m_ui.m_summaryTable->setRowCount(rowIndex + 1); m_ui.m_summaryTable->setItem(rowIndex, 0, new QTableWidgetItem(p_eventName)); for (int i = 0; i < p_eventDescription.size(); ++i) { m_ui.m_summaryTable->setItem(rowIndex, i + 1, new QTableWidgetItem(p_eventDescription[i])); } } }// addEvent(const QString p_eventName, const QStringList p_eventDescription) //--------------------------------------------------------------------------------------------------------- void HelpWidget::removeEvent(const QString p_eventName, const QStringList p_eventDescription) //--------------------------------------------------------------------------------------------------------- { int rowIndex = -1; for (int i = 0; i < m_ui.m_summaryTable->rowCount(); ++i) { // Search for a row with the same event name and same view if ((m_ui.m_summaryTable->item(i, 0)->text() == p_eventName) && (p_eventDescription.size()>=1) && (m_ui.m_summaryTable->item(i, 2)->text() == p_eventDescription[0])) { // Item found rowIndex = i; break; } } if (rowIndex >= 0) { // remove the row m_ui.m_summaryTable->removeRow(rowIndex); } }//removeEvent(const QString p_eventName)