3

How can I create a QDialog with floating toolbar in Qt?

Attachment of the QMainWindow with toolbar as widget in the QDialog is not suitable.

demonplus
  • 5,613
  • 12
  • 49
  • 68
mgukov
  • 493
  • 5
  • 18

2 Answers2

2

Why not suitable? following code works like charms.

#include <QtGui>

class MyDialog : public QDialog
{
    Q_OBJECT
public:
    MyDialog(QWidget* parent=0)
    {
        QMainWindow* child = new QMainWindow;
        QLabel* label = new QLabel(tr("QMainWindow with toolbar!"));
        label->setAlignment(Qt::AlignCenter);
        child->setCentralWidget(label);

        QToolBar* toolbar = child->addToolBar(tr("Tool"));
        toolbar->addAction(tr("Test"), this, SLOT(doTest()));

        QHBoxLayout* layout = new QHBoxLayout(this);
        layout->setContentsMargins(0,0,0,0);
        layout->addWidget(child);
    }
private slots:
    void doTest()
    {
        QMessageBox::information(this, tr("Test"), tr("ToolBar is Working!"));
    }
};
Joonhwan
  • 495
  • 5
  • 11
  • QMainWindow is too heavy. May be I'm not precisely formulated question. It is more correct : how can I create a Qwidget with floating toolbar in qt? if I want to create more than one toolbar for different widgets in one Dialog? – mgukov Oct 17 '12 at 09:20
  • Just floating only toolbar could be easily done because it is QWidget-derived and you can make QWidget as popup window any time I believe. But if you want make it docking/floating switchable, it seems there is no other choice except using QMainWindow. And one more thing. what do you mean by "QMainWindow is heavy?". Does QMainWindow works slow? – Joonhwan Oct 18 '12 at 23:59
0

look at Can you add a toolbar to QDialog? and try to write smth like that

myDialog->layout()->setMenuBar(myToolBar);
Community
  • 1
  • 1
std.approach
  • 51
  • 1
  • 7