mardi 24 novembre 2020

Why are 2 QToolBars overlapping?

I have been trying to solve an overlapping QWidget problem for the past few days, but no luck.

In particular I am creating multiple QToolBaras a separate classes via subclassing the QToolBar and inserting them into a QMainWindow.

Below you can see in the printscreen what is happening, the 2 QToolBar are overlapping each other. Also in addition the QToolBar does not extend and ends up with the ">>" but that should not happen because I should see all of them:

pic

Below the snippet of code:

mainwindow.cpp

MainWindow::MainWindow()
{
    createToolBars();
    resize(600, 600);

}
 
void MainWindow::createToolBars()
{
#if 1
    editTBars = new ToolBar::EditToolBar;
    editTBars->createEditToolBar("Edit ToolBar");
    addToolBar(editTBars);
    editTBars->setFixedHeight(36);
    editTBars->setMovable(true);
#endif

#if 1
    txtToolBar = new ToolBar::TextToolBar;
    txtToolBar->createTextToolBar("Text ToolBar");
    addToolBar(txtToolBar);
    txtToolBar->setFixedHeight(36);
    txtToolBar->setMovable(true);
#endif
}

globaltoolbars.h

namespace ToolBar {

    // -------------------- Edit ToolBar -------------------------
    // -----------------------------------------------------------

    class EditToolBar : public QToolBar
    {
    using ToolbarActionList = QList<QAction *>;

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

    private:
        QToolBar *editToolBar;
    };

    // -------------------- Text ToolBar ------------------------
    // ----------------------------------------------------------

    class TextToolBar : public QToolBar
    {
    using ToolbarActionTextList = QList<QAction *>;

    public:
        TextToolBar();
        ~TextToolBar();
        QToolBar *createTextToolBar(const QString &name);
        void textToolBarActions();    

    private:
        QToolBar *textToolBar;
        QComboBox *fontSizeCombo;
        QFontComboBox *fontCombo;

        QAction *boldAction;
        QAction *italicAction;
        QAction *underlineAction;

        template<class T>
        QList<QAction *> createActions(const T &);
    };
}

globaltoolbars.cpp

// ---------------------- Edit ToolBar -------------------------
// --------------------------------------------------------------

ToolBar::EditToolBar::EditToolBar(){}

ToolBar::EditToolBar::~EditToolBar(){}

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"),
                                               new QAction(QIcon(tr(":/imgaes/sendtoback.png")), "&Send To Back")}));
    return editToolBar;
}



// ---------------------- Text ToolBar -------------------------
// --------------------------------------------------------------


ToolBar::TextToolBar::TextToolBar() {}

ToolBar::TextToolBar::~TextToolBar() {}

void ToolBar::TextToolBar::textToolBarActions()
{
    boldAction = new QAction(tr("Bold"), this);
    boldAction->setCheckable(true);
    QPixmap pixmapBold(":/imgaes/bold.png");
    boldAction->setIcon(QIcon(pixmapBold));
    boldAction->setShortcut(tr("Ctrl+B"));

    italicAction = new QAction(tr("Italic"), this);
    italicAction->setCheckable(true);
    QPixmap pixmapItalic(":/imgaes/italic.png");
    italicAction->setIcon(QIcon(pixmapItalic));
    italicAction->setShortcut(tr("Ctrl+I"));

    underlineAction = new QAction(tr("Underline"), this);
    underlineAction->setCheckable(true);
    QPixmap pixmapUnderline(":/imgaes/underline.png");
    underlineAction->setIcon(pixmapUnderline);
    underlineAction->setShortcut(tr("Ctrl+U"));
}

QToolBar *ToolBar::TextToolBar::createTextToolBar(const QString &name)
{
    textToolBar = new QToolBar(name, this);
    fontCombo = new QFontComboBox;
    fontSizeCombo = new QComboBox;
    fontSizeCombo->setEditable(true);
    for(int i = 0; i < 30; i = i + 2) {
        fontSizeCombo->addItem(QString().setNum(i));
    }    
    textToolBar->addWidget(fontCombo);
    textToolBar->addWidget(fontSizeCombo);

    textToolBar->addActions(createActions("Text Actions"));

    return textToolBar;
}

template<class T>
QList<QAction *> ToolBar::TextToolBar::createActions(const T &)
{
    QList<QAction *> actions;
    QActionGroup *actionGroup = new QActionGroup(this);
    textToolBarActions();
    actions << boldAction << italicAction << underlineAction;
    QStringList namesOfActions;
    namesOfActions << tr("Bold") << tr("Italic") << tr("Underline");

    for(int i = 0; i < actions.count(); ++i) {
        QAction *action = new QAction(namesOfActions.at(i));
        actionGroup->addAction(action);
    }

    return actions;
}

What I have done so far:

In trying to solve this problem I came across this post which only describes the process via .ui forms but that is not what I have been trying to do here so it wasn't very helpfule.

Same is also happening here that describes via QVBoxLayout but that is not what I am trying to solve, although reading it was surely useful.

I found this additional source and went through it but that also was not very useful and also it mentions the use of QGridLayout, however I don't have (yet) a main layout on the user interface.

I decided to subclass the QToolBar for future modification in case there is need to add/remove some features and mostly to make the maintenance of the code more easy. And to brake it down in more components instead of having a one single mainwindow file with everything that could be very confusing.

I am not sure what is going on and why those QToolBars are being on top of each other.

Thanks for pointing to the right direction on how to solve this problem.

Aucun commentaire:

Enregistrer un commentaire