dimanche 22 novembre 2020

Why I can't see a QToolBar on QMainWindow

The problem I have is that I am not able to see a QToolBar as soon as the application is launched.

Because of the dimension of the GUI I created a specific class that handles all the creation of the QToolBar. I will have up to 7 QToolBars in total.

The strange thing I don't understand is that the member function of QToolBar is addWidget() and that takes a QWidget and in fact the ToolBar::GlobalToolBars I created inherits from a QToolBar which is a QWidget.

Below the code:

globaltoolbar.h

#include <QToolBar>
#include <QToolButton>

namespace ToolBar {
    using ToolbarActionList = QList<QAction *>;

    class GlobalToolBars : public QToolBar
    {
    public:
        GlobalToolBars();

        QToolBar *createEditToolBar(const QString &name);
        QAction *defineAction(const QString &path, const QString &name);


    private:
        QToolBar *editToolBar;

};
#endif // GLOBALTOOLBARS_H

globaltoolbar.cpp

#include "globaltoolbars.h"
#include <QGridLayout>

ToolBar::GlobalToolBars::GlobalToolBars(){}

QToolBar *ToolBar::GlobalToolBars::createEditToolBar(const QString &name)
{
    editToolBar = new QToolBar(name, this);
    editToolBar->addActions(ToolbarActionList({defineAction(":/imgaes/delete.png", "&Delete"),
                                               defineAction(":/imgaes/bringtofront.png", "&Bring To Front"),
                                               defineAction(":/imgaes/sendtoback.png", "&Send To Back")}));

    return editToolBar;
}


QAction *ToolBar::GlobalToolBars::defineAction(const QString &path, const QString &name)
{
    auto *action = new QAction(name);
    action->setIcon(QIcon(path));
    return action;
}

mainwindow.h

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();


private:
    ToolBar::GlobalToolBars *editTBar;
    void createToolBars();

};
#endif // MAINWINDOW_H

mainwindow.cpp

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    createToolBars();
    resize(600, 600);
}

void MainWindow::createToolBars()
{
    editTBar = new ToolBar::GlobalToolBars;
    editTBar->createEditToolBar("Edit");
    editTBar->addWidget(editTBar);
}

What I have tried so far:

In addition to the main example I posted above, I tried to subclass the QToolBar too but that didn't work and I still can't see the QToolBar, below the procedure I used for subclassing:

globaltoolbar.h

namespace ToolBar {
    using ToolbarActionList = QList<QAction *>;

    class EditToolBar : public QToolBar
    {
    public:
        EditToolBar();
        QToolBar *createEditToolBar(const QString &name);

    private:
        QToolBar *editToolBar;

    };
}

globaltoolbar.cpp

QToolBar *ToolBar::EditToolBar::createEditToolBar(const QString &name)
{
    editToolBar = new QToolBar(name, this);
    editToolBar->addActions(ToolbarActionList({new QAction(QIcon(tr(":/imgaes/delete.png")), "&Delete"),
                                               new QAction(QIcon(tr(":/imgaes/bringtofront.png")), "&Bring To Front"),
                                               new QAction(QIcon(tr(":/imgaes/sendtoback.png")), "&Send To Back")}));
    return editToolBar;
}

mainwindow.cpp

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    createToolBars();
    resize(600, 600);
}

void MainWindow::createToolBars()
{
    editTBar = new ToolBar::EditToolBar;
    editTBar->createEditToolBar("Edit");
    editTBar->addWidget(editTBar);
}

What am I missing?

Aucun commentaire:

Enregistrer un commentaire