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. How to set QComboBox menu background transparent?
Forum Updated to NodeBB v4.3 + New Features

How to set QComboBox menu background transparent?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.3k Views
  • 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.
  • R Offline
    R Offline
    Roberrt
    wrote on last edited by
    #1

    I'm trying to create a combo box with rounded borders including the pop-up menu, but when I set a border-radius, it doesn't apply to the background:

    Image demonstrating the issue

    While searching for a way to remove this background, I found this answer: https://stackoverflow.com/questions/27962162/rounded-qcombobox-without-square-box-behind

    To reproduce:

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
        ui.setupUi(this);
    
        QComboBox* comboBox = new QComboBox(this);
        
        comboBox->setStyleSheet(R"(
            QComboBox  {
                border-radius: 8px;
                margin: 10px;
                background-color: gray;
                min-height: 32px;
            }
        
            QComboBox QAbstractItemView {
                border-radius: 8px;
                margin: 10px;
                background-color: gray;
            }
        )");
        
        comboBox->move(100, 100);
        comboBox->addItem("1");
        comboBox->addItem("2");
        
        comboBox->view()->window()->setWindowFlags( Qt::Popup | Qt::FramelessWindowHint |Qt::NoDropShadowWindowHint);
        comboBox->view()->window()->setAttribute(Qt::WA_TranslucentBackground);
    
    }
    

    It does remove the background, but it lets a black background visible for a short:

    enter image description here

    How can I get rid of this black background?

    Here's everything I already tried that didn't work:

        if (comboBox->view()->parentWidget())
        {
            auto p = comboBox->view()->parentWidget();
            p->setWindowOpacity(0);
            p->setWindowFlags( Qt::Popup | Qt::FramelessWindowHint |Qt::NoDropShadowWindowHint);
            p->setAttribute(Qt::WA_TranslucentBackground);
            p->setHidden(true);
            p->setAutoFillBackground( false );
        }
    
        comboBox->setAutoFillBackground( false );
        comboBox->view()->setAutoFillBackground( false );
    	comboBox->view()->viewport()->setAutoFillBackground(false);
        comboBox->view()->window()->setAutoFillBackground(false);
    
        //comboBox->view()->setAttribute(Qt::WA_TranslucentBackground);
        //comboBox->view()->viewport()->setAttribute(Qt::WA_TranslucentBackground);
        //comboBox->view()->window()->setAttribute(Qt::WA_TranslucentBackground);
    
        comboBox->view()->setWindowOpacity(0);
        comboBox->view()->viewport()->setWindowOpacity(0);
        comboBox->view()->window()->setWindowOpacity(0);
    
        //comboBox->view()->setHidden(true);
        //comboBox->view()->viewport()->setHidden(true);
        //comboBox->view()->window()->setHidden(true);
    
        QPalette palette = comboBox->view()->palette();
        palette.setColor(QPalette::Window, Qt::transparent);
        comboBox->view()->setPalette(palette);
    

    The unique thing i didn't tried yet is using a proxy as i have no experience with it.

    Does someone has any idea if twerking into proxy it could be possible to get ride of that black window that appears for a short?

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Roberrt
      wrote on last edited by
      #2

      I couldn't even find a way to get a pointer to that window, which window is that?

      jsulmJ 1 Reply Last reply
      0
      • R Roberrt

        I couldn't even find a way to get a pointer to that window, which window is that?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @Roberrt said in How to set QComboBox menu background transparent?:

        I couldn't even find a way to get a pointer to that window

        What window do you mean?

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

        R 1 Reply Last reply
        0
        • jsulmJ jsulm

          @Roberrt said in How to set QComboBox menu background transparent?:

          I couldn't even find a way to get a pointer to that window

          What window do you mean?

          R Offline
          R Offline
          Roberrt
          wrote on last edited by Roberrt
          #4

          @jsulm the black window that appears when i click in the combo box, look at the gif, she appears just for 1 sec and then disappears.

          1a6280d5-27ae-4cc4-82c2-2a928c7204ab-image.png

          1 Reply Last reply
          0
          • R Offline
            R Offline
            Roberrt
            wrote on last edited by
            #5

            Does this window appear only to me?

            1 Reply Last reply
            0
            • R Offline
              R Offline
              Roberrt
              wrote on last edited by
              #6

              Does someone know if is possible to edt the popup background with a proxy style or something like?

              1 Reply Last reply
              0
              • H Offline
                H Offline
                HunterM
                wrote on last edited by
                #7

                If you're okay with disabling the animation part of the drop down, it will prevent the black box from appearing. You do this through QApplication::setEffectEnabled:

                QApplication::setEffectEnabled(Qt::UI_AnimateCombo, false);
                
                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