Frage

Ich erstelle einen Texteditor und ich möchte das platzieren QComboBox in dem QMenu. Ich habe keine Methode im Inneren gefunden QMenu Das hat so etwas umgegangen. Das nächste ist QMenu::addAction(). Ich wundte mich, diese Hürde umzugehen.

Vielen Dank!

War es hilfreich?

Lösung

Sie müssen Unterklasse QWidgetAction und dann einfach das rufen addAction auf Ihr Menü.

Beispielcode für Spin -Box -Aktion mit einem Etikett

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;
};

Erstellen Sie es jetzt einfach und fügen Sie es Ihrem Menü hinzu

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

Andere Tipps

QWidgetAction ist ein QAction das enthält a QWidget. Sie können dies verwenden, um Ihre zu verkapulieren QComboBox und fügen Sie es Ihrem Menü über hinzu QMenu::addAction.

Sie können immer a verwenden QWidget oder QFrame als Menü -Widget dann ein QHBoxLayout darauf und fügen Sie Ihre ein QWidgets Innerhalb.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top