Paul Jurczak
Paul Jurczak

Reputation: 8183

Qt menu bar without pull down menus (single level menu bar)

I would like to have a single level menu widget in Qt, which looks like a regular menu bar, but doesn't display pull down menu when an item on menu bar is clicked. My application needs only a few options and a single line menu bar with single click or single shortcut key activation seems to be the best fit.

I don't want a toolbar or a row of buttons. I don't want to design icons, I prefer a simple text for each option with shortcut key underlined.

Should I use QMenu or something else to implement this?

Upvotes: 1

Views: 1052

Answers (1)

Gombat
Gombat

Reputation: 2084

I'm sure, QToolBar is what you are looking for. It is a toolbar like you know from IDEs or Photoshop programs which shows options using icons or texts.

A code example

class MainWindow : public QMainWindow
{
  Q_OBJECT
public:
  MainWindow( QWidget* parent = 0 ) : QMainWindow(parent)
  {
    QToolBar* toolBar1 = new QToolBar(this);

    QAction* action1 = toolBar1->addAction("one");
    QObject::connect( action1, SIGNAL(triggered()), this, SLOT(onActionOne()));
    action1->setShortcut(QKeySequence("ctrl+o"));   

    QAction* action2 = toolBar1->addAction("two");
    QObject::connect( action2, SIGNAL(triggered()), this, SLOT(onActionTwo()));
    action2->setShortcut(QKeySequence("ctrl+t"));

    addToolBar(Qt::TopToolBarArea, toolBar1);
  }

public slots:
  void onActionOne(){ std::cout << "Action one!" << std::endl; }
  void onActionTwo(){ std::cout << "Action Two!" << std::endl; }
};

Upvotes: 1

Related Questions