1

I am trying to implement a popup menu on QPushButton using QComboBox popup function.

When I click on the button a menu appears but their is some space between the button and the menu. How do I remove that?

I also want the text color to remain fixed when I hover on an item in the menu.

How do i do that?

I am trying to solve this already for 3 days but not getting any solution by CSS nor any QComboBox function.

Thank you.

Kurt Pattyn
  • 2,758
  • 2
  • 30
  • 42
bhupinder
  • 315
  • 1
  • 6
  • 23
  • Can you elaborate a little on how are you exacly combining `QPushButton` and `QComboBox`? By the way, why implementing this using `QToolButton` with `popupMode` - `MenuButtonPopup` and using `setMenu` isn't good for you? – Predelnik Feb 02 '14 at 14:32
  • I am implementing QComboBox *combo = new QComboBox(ui.mUploadButton); I didn't try QToolButton unfamiliar with many classes in QT – bhupinder Feb 02 '14 at 14:38
  • Well honestly what you're doing isn't such a good practice because you're trying to place widget inside the one which isn't even container widget (you can see list of container widgets in Qt designer for example), this at least most likely will lead you to creation of non-intuitive user controls. I suggest to try QToolButton with menu which is more or less standard. By the way why do you need text color to remain fixed on hover? With default windows background hover color text will become barely visible if it will stay black. – Predelnik Feb 02 '14 at 14:56
  • Its requirement that why i need a way to make hover text color remain fix – bhupinder Feb 02 '14 at 15:04
  • Well I'm not sure that it's what you truly want but you may look into this [answer](http://stackoverflow.com/a/21446953/1269661) regarding your second question – Predelnik Feb 02 '14 at 15:42
  • no qtoolbutton is not good choice for me at present stage .i treid – bhupinder Feb 03 '14 at 06:51

1 Answers1

1

You can use a QToolButton instead of QPushButton and add actions to the QToolButton. You should create your custom QWidgetAction to add to the popup menu.

This is sample QWidgetAction:

#include <QWidgetAction>

class  myCustomWidgetAction: public QWidgetAction
{
    Q_OBJECT
public:
    explicit myCustomWidgetAction(QWidget * parent);

protected:
    QWidget * createWidget(QWidget *parent);

};


myCustomWidgetAction::myCustomWidgetAction(QWidget * parent):QWidgetAction(parent) {
}
QWidget * myCustomWidgetAction::createWidget(QWidget *parent){
    myCustomWidget * widget=new myCustomWidget(parent);
    return widget;
}

You can then add your widget to the toolButton to be diaplayed in a popup menu:

myCustomWidgetAction * widgetAction   = new myCustomWidgetAction(this);

ui->toolButton->addAction(widgetAction);

Your custom widget can be a list containing different elements or it can be any other widget. You can also add multiple instances of myCustomWidgetAction to the toolButton.

Nejat
  • 31,784
  • 12
  • 106
  • 138