Can you add a toolbar to QDialog?

后端 未结 3 828
自闭症患者
自闭症患者 2020-12-17 18:29

I\'m working on a project that needs to call a modal window with a toolbar to do some work on some data before it\'s loaded. The reason I need the toolbar is the user has a

相关标签:
3条回答
  • 2020-12-17 18:44

    You can simply use the setMenuBar function of the layout manager that is installed on your QDialog:

    myDialog->layout()->setMenuBar(myMenuBar);
    
    0 讨论(0)
  • 2020-12-17 18:47

    If you don't need the built-in drag and drop feature of QMainWindow's toolbars, you can simply add a QToolBar to any layout, including QDialog's layout(). See the DigviJay Patil's answer below for details, which is definitely cleaner conceptually.

    Otherwise, please read on.


    1. It is not directly possible to add a QToolBar to a QDialog in the QMainWindow::addToolBar() sense, because QDialog inherits only QWidget and not QMainWindow, as you noted (hence do not have the method addToolBar())

    2. You can't make a QMainWindow modal, but you can insert a QMainWindow in a QDialog this way:

    Code:

    MyDialog::MyDialog() :
        QDialog()
    {
        QMainWindow * mainWindow = new QMainWindow(); // or your own class
                                                      // inheriting QMainWindow
    
        QToolBar * myToolBar = new QToolBar();
        mainWindow->addToolBar(myToolBar);
    
        QHBoxLayout * layout = new QHBoxLayout();
        layout->addWidget(mainWindow);
        setLayout(layout);
    }
    

    Indeed, a QMainWindow doesn't necessarily have to be a top-level widget, and you can even insert several QMainWindows as children of a single widget (may not be the wisest choice though, as the user would probably be confused with the separate sets of menu bars, toolbars, dock widgets, etc.).

    0 讨论(0)
  • 2020-12-17 19:00

    You can add QToolBar in QDialog. But as a QWidget. Please have a look

    MyDialog::MyDialog(QWidget *parent) : QDialog(parent)
    {
       QVBoxLayout *mainLayout = new QVBoxLayout(this);
    
       QToolBar *toolBar = new QToolBar();
       mainLayout->addWidget(toolBar);
    
       QAction *action1 = new QAction("Add", toolBar);
       QAction *action1 = new QAction("Del", toolBar);
    
      //Add What you want
    }
    

    As QToolBar is child of QWidget we can add it as Widget. Using Layout you can adjust its position. Please check this link http://developer.nokia.com/community/wiki/How_to_use_QToolBar_and_QToolButton_in_Qt

    0 讨论(0)
提交回复
热议问题