Stylesheet on custom tableview
Solved
General and Desktop
-
Hi,
I made a table view with an item delegate.
I also redifine paint event like this:void MdCommandTableView::paintEvent(QPaintEvent* event) { if(!m_pixBackgroundBottom.isNull()) { QPainter painter(viewport()); QRect rect(viewport()->rect()); painter.fillRect(rect, Qt::white); if(height() < width()) { int iHeight = height() / 1.5; int iWidth = ((double)m_pixBackgroundBottom.size().width() / (double)m_pixBackgroundBottom.size().height()) * iHeight; painter.drawPixmap(0, height() - iHeight, iWidth,iHeight, m_pixBackgroundBottom); iWidth = ((double)m_pixBackgroundTop.size().width() / (double)m_pixBackgroundTop.size().height()) * iHeight; painter.drawPixmap(width() - iWidth, 0, iWidth,iHeight, m_pixBackgroundTop); } else { int iWidth = width(); int iHeight = ((double)m_pixBackgroundBottom.size().height() / (double)m_pixBackgroundBottom.size().width()) * (double)iWidth; painter.drawPixmap(0, height() - iHeight, iWidth,iHeight, m_pixBackgroundBottom); iHeight = ((double)m_pixBackgroundTop.size().height() / (double)m_pixBackgroundTop.size().width()) * (double)iWidth; painter.drawPixmap(width() - iWidth, 0, iWidth,iHeight, m_pixBackgroundTop); } painter.end(); } QTableView::paintEvent(event); }
I would like to customize the scrollbar with a stylesheets:
QScrollBar:horizontal { border: 1px solid #999999; background:white; width:10px; border-radius: 5px; margin: 0px 0px 0px 0px; } QScrollBar::handle:horizontal { background: qlineargradient(x1:0, y1:0, x2:1, y2:0, stop: 0 rgb(150, 150, 150), stop: 0.5 rgb(130, 130, 130), stop:1 rgb(114, 114, 114)); min-height: 0px; border-radius: 5px; } QScrollBar::add-line:horizontal { background: qlineargradient(x1:0, y1:0, x2:1, y2:0, stop: 0 rgb(150, 150, 150), stop: 0.5 rgb(130, 130, 130), stop:1 rgb(114, 114, 114)); height: 0px; subcontrol-position: bottom; subcontrol-origin: margin; } QScrollBar::sub-line:horizontal { background: qlineargradient(x1:0, y1:0, x2:1, y2:0, stop: 0 rgb(150, 150, 150), stop: 0.5 rgb(130, 130, 130), stop:1 rgb(114, 114, 114)); height: 0 px; subcontrol-position: top; subcontrol-origin: margin; }
But with stylesheet the scrollbars are never displayed...
Ho can I draw the scrollbars ?thanks,
Sorry for my english -
Hi,
Can you also show how you setup your QTableView subclass ?
-
I implement my table view like this:
MdCommandTableView::MdCommandTableView(const QString sTitle, QWidget *parent) : QTableView(parent), m_title{sTitle}, m_pixBackgroundBottom{":/resources/res/res/carrebas2.png"}, m_pixBackgroundTop{":/resources/res/res/carrehaut2.png"} { setMouseTracking(true); setSelectionMode(QAbstractItemView::SingleSelection); setSelectionBehavior(QAbstractItemView::SelectItems); setShowGrid(false); setItemDelegate(new MdCommandsItemDelegate(this)); setModel(new MdCommandsModel(this)); horizontalHeader()->setVisible(false); verticalHeader()->setVisible(false); QObject::connect(this, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(onDoubleClicked(QModelIndex))); initSizes(); QObject::connect(horizontalScrollBar(), SIGNAL(valueChanged(int)), this->viewport(), SLOT(update())); QObject::connect(this, SIGNAL(sg_command(QString, const OdEdCommand*)), MdCoreApplication::Instance(), SLOT(onExecCmd(QString, const OdEdCommand*)), Qt::QueuedConnection); QString sStyleSheet("QScrollBar:vertical {" " border: 1px solid #999999;" " background:white;" " width:10px; " " border-radius: 5px;" " margin: 0px 0px 0px 0px;" "}" "QScrollBar::handle:vertical {" " background: qlineargradient(x1:0, y1:0, x2:1, y2:0," " stop: 0 rgb(150, 150, 150), stop: 0.5 rgb(130, 130, 130), stop:1 rgb(114, 114, 114));" " min-height: 0px;" " border-radius: 5px;" "}" "QScrollBar::add-line:vertical {" " background: qlineargradient(x1:0, y1:0, x2:1, y2:0," " stop: 0 rgb(150, 150, 150), stop: 0.5 rgb(130, 130, 130), stop:1 rgb(114, 114, 114));" " height: 0px;" " subcontrol-position: bottom;" " subcontrol-origin: margin;" "}" "QScrollBar::sub-line:vertical {" " background: qlineargradient(x1:0, y1:0, x2:1, y2:0," " stop: 0 rgb(150, 150, 150), stop: 0.5 rgb(130, 130, 130), stop:1 rgb(114, 114, 114));" " height: 0 px;" " subcontrol-position: top;" " subcontrol-origin: margin;" "}" "QScrollBar:horizontal {" " border: 1px solid #999999;" " background:white;" " width:10px; " " border-radius: 5px;" " margin: 0px 0px 0px 0px;" "}" "QScrollBar::handle:horizontal {" " background: qlineargradient(x1:0, y1:0, x2:1, y2:0," " stop: 0 rgb(150, 150, 150), stop: 0.5 rgb(130, 130, 130), stop:1 rgb(114, 114, 114));" " min-height: 0px;" " border-radius: 5px;" "}" "QScrollBar::add-line:horizontal {" " background: qlineargradient(x1:0, y1:0, x2:1, y2:0," " stop: 0 rgb(150, 150, 150), stop: 0.5 rgb(130, 130, 130), stop:1 rgb(114, 114, 114));" " height: 0px;" " subcontrol-position: bottom;" " subcontrol-origin: margin;" "}" "QScrollBar::sub-line:horizontal {" " background: qlineargradient(x1:0, y1:0, x2:1, y2:0," " stop: 0 rgb(150, 150, 150), stop: 0.5 rgb(130, 130, 130), stop:1 rgb(114, 114, 114));" " height: 0 px;" " subcontrol-position: top;" " subcontrol-origin: margin;" "}"); setStyleSheet(sStyleSheet); } MdCommandTableView::~MdCommandTableView() { // } QString MdCommandTableView::title()const { return m_title; } void MdCommandTableView::setupModel(const QMap<uchar, MdCustomizationUIElement *> &qmCustomElemByIdentifier) { MdCommandsModel* pCmdModel = (MdCommandsModel*) model(); pCmdModel->setupModel(qmCustomElemByIdentifier); } void MdCommandTableView::setupModel(const QList<MdCommandAction *> &qlAct) { MdCommandsModel* pCmdModel = (MdCommandsModel*) model(); pCmdModel->setupModel(qlAct); } void MdCommandTableView::setupModel(const QList<MdCustomizationUIElement *> &qlCustomElem) { MdCommandsModel* pCmdModel = (MdCommandsModel*) model(); pCmdModel->setupModel(qlCustomElem); } void MdCommandTableView::initSizes() { QHeaderView* pVHeader = verticalHeader(); QHeaderView* pHHeader = horizontalHeader(); pVHeader->setMaximumSectionSize(CMD_ITEM_SIZE); pVHeader->setMinimumSectionSize(CMD_ITEM_SIZE); pHHeader->setMaximumSectionSize(CMD_ITEM_SIZE); pHHeader->setMinimumSectionSize(CMD_ITEM_SIZE); pVHeader->setDefaultSectionSize(CMD_ITEM_SIZE); pHHeader->setDefaultSectionSize(CMD_ITEM_SIZE); } void MdCommandTableView::updateRowCount() { int rowCount = height() / CMD_ITEM_SIZE; MdCommandsModel* pCmdModel = (MdCommandsModel*) model(); if(pCmdModel) pCmdModel->onChangeRowCount(rowCount); } void MdCommandTableView::onDoubleClicked(QModelIndex index) { QString sCmd = model()->data(index, Qt::UserRole + 1).toString(); emit sg_command(sCmd, nullptr); } void MdCommandTableView::resizeEvent(QResizeEvent *event) { int rowCount = event->size().height() / CMD_ITEM_SIZE; MdCommandsModel* pCmdModel = (MdCommandsModel*) model(); if(pCmdModel) { int iIndex = -1; if(!selectedIndexes().isEmpty()) { iIndex = pCmdModel->cmdIndex(selectedIndexes()[0]); } pCmdModel->onChangeRowCount(rowCount); QTableView::resizeEvent(event); if(iIndex >= 0) { QItemSelectionModel *pSelectModel = selectionModel(); pSelectModel->select(pCmdModel->cmdModelIndex(iIndex), QItemSelectionModel::Select); setSelectionModel(pSelectModel); } } else { QTableView::resizeEvent(event); } } void MdCommandTableView::wheelEvent(QWheelEvent* event) { QScrollBar *pHScrollbar = horizontalScrollBar(); if(pHScrollbar) { if(pHScrollbar->isVisible()) { QApplication::sendEvent(pHScrollbar, event); } } } void MdCommandTableView::leaveEvent(QEvent *event) { emit entered(QModelIndex()); QTableView::leaveEvent(event); } void MdCommandTableView::mouseMoveEvent(QMouseEvent *event) { QModelIndex hoveredIndex = indexAt(event->pos()); if(model()) { QString sDesignation = model()->data(hoveredIndex, Qt::UserRole + 1).toString(); if(!sDesignation.isEmpty()) { emit sg_cmdInfo(sDesignation); } else { emit sg_cmdInfo(""); emit entered(hoveredIndex); } } QTableView::mouseMoveEvent(event); } void MdCommandTableView::showEvent(QShowEvent* event) { updateRowCount(); QTableView::showEvent(event); } void MdCommandTableView::paintEvent(QPaintEvent* event) { if(!m_pixBackgroundBottom.isNull()) { QPainter painter(viewport()); QRect rect(viewport()->rect()); painter.fillRect(rect, Qt::white); if(height() < width()) { int iHeight = height() / 1.5; int iWidth = ((double)m_pixBackgroundBottom.size().width() / (double)m_pixBackgroundBottom.size().height()) * iHeight; painter.drawPixmap(0, height() - iHeight, iWidth,iHeight, m_pixBackgroundBottom); iWidth = ((double)m_pixBackgroundTop.size().width() / (double)m_pixBackgroundTop.size().height()) * iHeight; painter.drawPixmap(width() - iWidth, 0, iWidth,iHeight, m_pixBackgroundTop); } else { int iWidth = width(); int iHeight = ((double)m_pixBackgroundBottom.size().height() / (double)m_pixBackgroundBottom.size().width()) * (double)iWidth; painter.drawPixmap(0, height() - iHeight, iWidth,iHeight, m_pixBackgroundBottom); iHeight = ((double)m_pixBackgroundTop.size().height() / (double)m_pixBackgroundTop.size().width()) * (double)iWidth; painter.drawPixmap(width() - iWidth, 0, iWidth,iHeight, m_pixBackgroundTop); } painter.end(); } QTableView::paintEvent(event); } void MdCommandTableView::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) { QTableView::selectionChanged(selected, deselected); if(selectedIndexes().isEmpty()) { emit clicked(QModelIndex()); } else { emit clicked(selectedIndexes()[0]); } }
-
Do you have that code available somewhere ?
The model setup is missing and its difficult to reproduce what is happening to you.
Also, which version of Qt are you using ?
On which OS ? -
Glad you found out and thanks for sharing !
I was about to write that on further look there where zeros in your stylesheet but you were faster :)