Pregunta

Estoy creando un editor de texto y me gustaría poner el QComboBox en el QMenu. No encontré ningún método dentro del QMenu Eso manejó tal cosa. Lo más cercano está QMenu::addAction(). Me preguntaba al evitar este obstáculo.

¡Gracias!

¿Fue útil?

Solución

Tienes que subclase QWidgetAction y luego simplemente llame al addAction a tu menú.

Código de ejemplo para la acción de la caja de spins con una etiqueta

class SpinBoxAction : public QWidgetAction {
public:
    SpinBoxAction (const QString& title) : 
      QWidgetAction (NULL) {
        QWidget* pWidget = new QWidget (NULL);
        QHBoxLayout* pLayout = new QHBoxLayout();
        QLabel* pLabel = new QLabel (title);  //bug fixed here, pointer was missing
        pLayout->addWidget (pLabel);
        pSpinBox = new QSpinBox(NULL);
        pLayout->addWidget (pSpinBox);
        pWidget->setLayout (pLayout);

        setDefaultWidget(pWidget);
    }

    QSpinBox * spinBox () {
        return pSpinBox;
    }

private:
    QSpinBox * pSpinBox;
};

Ahora simplemente cree y agrégalo a su menú

SpinBoxAction * spinBoxAction = new SpinBoxAction(tr("Action Title"));
// make a connection
connect(spinBoxAction ->spinBox(), SIGNAL(valueChanged(int)), 
        this, SLOT(spinboxValueChanged(int)));
// add it to your menu
menu->addAction(spinBoxAction);

Otros consejos

QWidgetAction es un QAction que contiene un QWidget. Puedes usar esto para encapsular tu QComboBox y agrégalo a su menú a través de QMenu::addAction.

Siempre puedes usar un QWidget o QFrame Como widget del menú, luego coloque un QHBoxLayout sobre él e inserte su QWidgets en el interior.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top