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. Stylesheet on custom tableview
Qt 6.11 is out! See what's new in the release blog

Stylesheet on custom tableview

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 2.8k Views 1 Watching
  • 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.
  • Roy44R Offline
    Roy44R Offline
    Roy44
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Can you also show how you setup your QTableView subclass ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Roy44R Offline
        Roy44R Offline
        Roy44
        wrote on last edited by
        #3

        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]);
            }
        }
        
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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 ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • Roy44R Offline
            Roy44R Offline
            Roy44
            wrote on last edited by
            #5

            Sorry the problem is my stylesheets:
            height: 0px; for horizontal scrollbar

            1 Reply Last reply
            1
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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 :)

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

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