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. Set Color and background color of childs of custom QWidgetAction
Forum Updated to NodeBB v4.3 + New Features

Set Color and background color of childs of custom QWidgetAction

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 3 Posters 2.2k 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.
  • H Offline
    H Offline
    hamzeh.nasajpour
    wrote on last edited by
    #1

    Hi guys.

    I implemented a custom QWidgetAction included:

    1. one QIcon in the left
    2. two QLabel in the middle, vertically
    3. one QLabel in the right

    I want to change the text color and background color of all QLabels on hover. I'm using the qss for setting the stylesheet. Do you have any idea whats the problem? or ...?

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

      Hi and welcome to devnet,

      What is your issue ?
      Which version of Qt ?
      On which OS.?
      Can you provide a minimal compilable example that shows your issue ?

      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
      • H Offline
        H Offline
        hamzeh.nasajpour
        wrote on last edited by
        #3

        @SGaist Sorry for delay.

        This is qss:

        ...
        
        #customitem {
            color: palette(text);
            border: none;
        }
        
        #customitem > QWidget{
            color: palette(text);
        }
        
        #customitem:hover {
            background: palette(highlight);
            color: white;
        }
        

        and this is in my widget action:

        ...
            auto qlayout = new QHBoxLayout;
            qlayout->addLayout(llayout);
            qlayout->addLayout(rlayout);
        
            auto  widget = new QWidget;
            widget->setObjectName("customitem");
            widget->setLayout(qlayout);
            auto qWidgetAction = new QWidgetAction(this);
            qWidgetAction->setDefaultWidget(widget);
        
        

        The background of the widget will change on hover but text colors of labels no.

        1 Reply Last reply
        0
        • H Offline
          H Offline
          hamzeh.nasajpour
          wrote on last edited by
          #4

          This is my codes:

          #include <QDebug>
          #include <QString>
          #include <QWidgetAction>
          #include <QHBoxLayout>
          #include <QLabel>
          #include <QFont>
          #include <QtGui/QDesktopServices>
          #include <QUrl>
          #include <QMouseEvent>
          
          
          class ResultItem : public QWidgetAction{
          Q_OBJECT
          public:
              ResultItem(const QString &name, QObject *parent = nullptr)
                      : QWidgetAction(parent) {
                  QLabel *iLabel = new QLabel("*");
                  QLabel *mUpLabel = new QLabel("UP Center " + name);
                  mUpLabel->setAlignment(Qt::AlignLeft);
          
                  QLabel *mDownLabel = new QLabel("Down Center");
                  mDownLabel->setAlignment(Qt::AlignLeft);
          
                  QLabel *timeLabel = new QLabel("20201015 - 10:15");
          
                  auto *mLayout = new QVBoxLayout;
                  mLayout->addWidget(mUpLabel);
                  mLayout->addWidget(mDownLabel);
                  mLayout->setAlignment(Qt::AlignLeft);
          
                  auto *mainLayout = new QHBoxLayout;
                  mainLayout->addWidget(iLabel);
                  mainLayout->addLayout(mLayout);
                  mainLayout->addWidget(timeLabel);
                  mainLayout->setAlignment(Qt::AlignLeft);
          
                  auto *widget = new QWidget;
                  widget->setLayout(mainLayout);
                  widget->setStyleSheet("QWidget:hover {background: blue; color: white;}");
          
                  setDefaultWidget(widget);
                  setText(name);
              }
          };
          
          

          and this is the result in GUI:
          ScreenShot-2020-10-18_12-09-49.png

          ScreenShot-2020-10-18_12-10-13.png

          As you see the style doesn't apply on the whole of the qwidgetaction. The color of text in labels won't be changed but when I hover on the label the the text colors update.

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

            You are showing two different pieces of code. As requested please provide a complete minimal example that shows the behaviour.

            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
            • H Offline
              H Offline
              hamzeh.nasajpour
              wrote on last edited by hamzeh.nasajpour
              #6
              • main.cpp
              #include <QApplication>
              #include "mainwindow.h"
              
              int main(int argc, char *argv[])
              {
                  QApplication app(argc, argv);
                  MainWindow window;
                  window.show();
                  return app.exec();
              }
              
              • mainwindow.cpp
              #include <QtWidgets>
              #include "mainwindow.h"
              
              MainWindow::MainWindow()
              {
                  QWidget *widget = new QWidget;
                  setCentralWidget(widget);
                  QWidget *topFiller = new QWidget;
                  topFiller->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
              
                  infoLabel = new QLabel(tr("<i>Choose a menu option, or right-click to "
                                            "invoke a context menu</i>"));
                  infoLabel->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
                  infoLabel->setAlignment(Qt::AlignCenter);
              
                  QWidget *bottomFiller = new QWidget;
                  bottomFiller->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
              
                  QVBoxLayout *layout = new QVBoxLayout;
                  layout->setContentsMargins(5, 5, 5, 5);
                  layout->addWidget(topFiller);
                  layout->addWidget(infoLabel);
                  layout->addWidget(bottomFiller);
                  widget->setLayout(layout);
                  createActions();
                  createMenus();
              
                  QString message = tr("A context menu is available by right-clicking");
                  statusBar()->showMessage(message);
              
                  setWindowTitle(tr("Menus"));
                  setMinimumSize(160, 160);
                  resize(480, 320);
              }
              
              void MainWindow::createActions()
              {
                  exitAct = new QAction(tr("E&xit"), this);
                  exitAct->setShortcuts(QKeySequence::Quit);
                  exitAct->setStatusTip(tr("Exit the application"));
                  connect(exitAct, &QAction::triggered, this, &QWidget::close);
              
              }
              
              void MainWindow::createMenus()
              {
                  fileMenu = menuBar()->addMenu(tr("&File"));
                  auto ri = new ResultItem("CUSTOM", this);
                  fileMenu->addAction(ri);
                  fileMenu->addAction(exitAct);
              }
              
              • mainwindow.h
              
              #ifndef MAINWINDOW_H
              #define MAINWINDOW_H
              
              #include <QMainWindow>
              #include "ResultItem.h"
              
              QT_BEGIN_NAMESPACE
              class QAction;
              class QActionGroup;
              class QLabel;
              class QMenu;
              QT_END_NAMESPACE
              
              class MainWindow : public QMainWindow
              {
                  Q_OBJECT
              
              public:
                  MainWindow();
              
              private:
                  void createActions();
                  void createMenus();
              
                  QMenu *fileMenu;
                  QAction *exitAct;
                  QLabel *infoLabel;
              };
              
              #endif
              
              • menus.pro
              QT += widgets
              
              HEADERS       = mainwindow.h ResultItem.h
              SOURCES       = mainwindow.cpp \
                              main.cpp
              
              # install
              target.path = $$[QT_INSTALL_EXAMPLES]/widgets/mainwindows/menus
              INSTALLS += target
              
              • ResultItem.h
              #ifndef RESULTITEM_H
              #define RESULTITEM_H
              
              #include <QDebug>
              #include <QString>
              #include <QWidgetAction>
              #include <QHBoxLayout>
              #include <QLabel>
              #include <QFont>
              #include <QtGui/QDesktopServices>
              #include <QUrl>
              #include <QMouseEvent>
              
              
              class ResultItem : public QWidgetAction{
              Q_OBJECT
              public:
                  ResultItem(const QString &name, QObject *parent = nullptr)
                          : QWidgetAction(parent) {
                      QLabel *iLabel = new QLabel("*");
                      QLabel *mUpLabel = new QLabel("UP Center " + name);
                      mUpLabel->setAlignment(Qt::AlignLeft);
              
                      QLabel *mDownLabel = new QLabel("Down Center");
                      mDownLabel->setAlignment(Qt::AlignLeft);
              
                      QLabel *timeLabel = new QLabel("20201015 - 10:15");
              
                      auto *mLayout = new QVBoxLayout;
                      mLayout->addWidget(mUpLabel);
                      mLayout->addWidget(mDownLabel);
                      mLayout->setAlignment(Qt::AlignLeft);
              
                      auto *mainLayout = new QHBoxLayout;
                      mainLayout->addWidget(iLabel);
                      mainLayout->addLayout(mLayout);
                      mainLayout->addWidget(timeLabel);
                      mainLayout->setAlignment(Qt::AlignLeft);
              
                      auto *widget = new QWidget;
                      widget->setLayout(mainLayout);
                      // widget->setStyleSheet("QWidget { background-color: yellow }");
                      // widget->setStyleSheet("QWidget {background: yellow; color: blue;}");
                      widget->setStyleSheet("QWidget:hover {background: blue; color: white;}");
              
                      setDefaultWidget(widget);
                      setText(name);
                  }
              };
              
              
              #endif
              
              jsulmJ 1 Reply Last reply
              0
              • H hamzeh.nasajpour
                • main.cpp
                #include <QApplication>
                #include "mainwindow.h"
                
                int main(int argc, char *argv[])
                {
                    QApplication app(argc, argv);
                    MainWindow window;
                    window.show();
                    return app.exec();
                }
                
                • mainwindow.cpp
                #include <QtWidgets>
                #include "mainwindow.h"
                
                MainWindow::MainWindow()
                {
                    QWidget *widget = new QWidget;
                    setCentralWidget(widget);
                    QWidget *topFiller = new QWidget;
                    topFiller->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                
                    infoLabel = new QLabel(tr("<i>Choose a menu option, or right-click to "
                                              "invoke a context menu</i>"));
                    infoLabel->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
                    infoLabel->setAlignment(Qt::AlignCenter);
                
                    QWidget *bottomFiller = new QWidget;
                    bottomFiller->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                
                    QVBoxLayout *layout = new QVBoxLayout;
                    layout->setContentsMargins(5, 5, 5, 5);
                    layout->addWidget(topFiller);
                    layout->addWidget(infoLabel);
                    layout->addWidget(bottomFiller);
                    widget->setLayout(layout);
                    createActions();
                    createMenus();
                
                    QString message = tr("A context menu is available by right-clicking");
                    statusBar()->showMessage(message);
                
                    setWindowTitle(tr("Menus"));
                    setMinimumSize(160, 160);
                    resize(480, 320);
                }
                
                void MainWindow::createActions()
                {
                    exitAct = new QAction(tr("E&xit"), this);
                    exitAct->setShortcuts(QKeySequence::Quit);
                    exitAct->setStatusTip(tr("Exit the application"));
                    connect(exitAct, &QAction::triggered, this, &QWidget::close);
                
                }
                
                void MainWindow::createMenus()
                {
                    fileMenu = menuBar()->addMenu(tr("&File"));
                    auto ri = new ResultItem("CUSTOM", this);
                    fileMenu->addAction(ri);
                    fileMenu->addAction(exitAct);
                }
                
                • mainwindow.h
                
                #ifndef MAINWINDOW_H
                #define MAINWINDOW_H
                
                #include <QMainWindow>
                #include "ResultItem.h"
                
                QT_BEGIN_NAMESPACE
                class QAction;
                class QActionGroup;
                class QLabel;
                class QMenu;
                QT_END_NAMESPACE
                
                class MainWindow : public QMainWindow
                {
                    Q_OBJECT
                
                public:
                    MainWindow();
                
                private:
                    void createActions();
                    void createMenus();
                
                    QMenu *fileMenu;
                    QAction *exitAct;
                    QLabel *infoLabel;
                };
                
                #endif
                
                • menus.pro
                QT += widgets
                
                HEADERS       = mainwindow.h ResultItem.h
                SOURCES       = mainwindow.cpp \
                                main.cpp
                
                # install
                target.path = $$[QT_INSTALL_EXAMPLES]/widgets/mainwindows/menus
                INSTALLS += target
                
                • ResultItem.h
                #ifndef RESULTITEM_H
                #define RESULTITEM_H
                
                #include <QDebug>
                #include <QString>
                #include <QWidgetAction>
                #include <QHBoxLayout>
                #include <QLabel>
                #include <QFont>
                #include <QtGui/QDesktopServices>
                #include <QUrl>
                #include <QMouseEvent>
                
                
                class ResultItem : public QWidgetAction{
                Q_OBJECT
                public:
                    ResultItem(const QString &name, QObject *parent = nullptr)
                            : QWidgetAction(parent) {
                        QLabel *iLabel = new QLabel("*");
                        QLabel *mUpLabel = new QLabel("UP Center " + name);
                        mUpLabel->setAlignment(Qt::AlignLeft);
                
                        QLabel *mDownLabel = new QLabel("Down Center");
                        mDownLabel->setAlignment(Qt::AlignLeft);
                
                        QLabel *timeLabel = new QLabel("20201015 - 10:15");
                
                        auto *mLayout = new QVBoxLayout;
                        mLayout->addWidget(mUpLabel);
                        mLayout->addWidget(mDownLabel);
                        mLayout->setAlignment(Qt::AlignLeft);
                
                        auto *mainLayout = new QHBoxLayout;
                        mainLayout->addWidget(iLabel);
                        mainLayout->addLayout(mLayout);
                        mainLayout->addWidget(timeLabel);
                        mainLayout->setAlignment(Qt::AlignLeft);
                
                        auto *widget = new QWidget;
                        widget->setLayout(mainLayout);
                        // widget->setStyleSheet("QWidget { background-color: yellow }");
                        // widget->setStyleSheet("QWidget {background: yellow; color: blue;}");
                        widget->setStyleSheet("QWidget:hover {background: blue; color: white;}");
                
                        setDefaultWidget(widget);
                        setText(name);
                    }
                };
                
                
                #endif
                
                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @hamzeh-nasajpour You really think this is MINIMAL example code reproducing the issue?!
                Also, please format your code properly.

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

                1 Reply Last reply
                2
                • H Offline
                  H Offline
                  hamzeh.nasajpour
                  wrote on last edited by
                  #8

                  @jsulm You're right. I removed unused codes.
                  @SGaist The above post updated, the issue can be reproduced by above codes.

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

                    Just to be sure I understand your issue, you want that in case you hover over any part of the widget that all its elements changes colour at the same time ?

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

                    H 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      Just to be sure I understand your issue, you want that in case you hover over any part of the widget that all its elements changes colour at the same time ?

                      H Offline
                      H Offline
                      hamzeh.nasajpour
                      wrote on last edited by
                      #10

                      Just to be sure I understand your issue, you want that in case you hover over any part of the widget that all its elements changes colour at the same time ?

                      @SGaist Yes, exactly. Hover over any part --> Change the text color and background color of all parts of related items.

                      1 Reply Last reply
                      0
                      • H Offline
                        H Offline
                        hamzeh.nasajpour
                        wrote on last edited by
                        #11

                        any idea?

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

                          You can't directly apply your hover background to widgets that are not being hovered.

                          One thing you can do is to use a dynamic property and set it to all the widgets when you entering the main one and change its value when moving out of it.

                          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

                          • Login

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