如何以编程方式将工具按钮移动到右侧工具栏区域

How to move programmatically tool buttons to right toolbar area?

本文关键字:移动 区域 工具栏 按钮 工具 编程 方式      更新时间:2023-10-16

我希望工具图标在右侧,而不是顶部。正如我所经历的,我可以手动移动它们,我可以将它们orientation()设置为垂直,但它们仍位于顶部;我可以设置setAllowedAreas()这意味着我限制了工具栏区域可以驻留的位置,但工具按钮位于顶部。我需要像setToolbarArea()这样的东西.有类似的东西吗?

您可以再次调用addToolBar来移动工具栏。

根据文件,

如果主窗口已经管理工具栏,那么它只会移动 工具栏到区域。

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    Ui::MainWindow *ui;
    QToolBar * toolBar;
public slots:
    void moveLeft();
    void moveRight();
};
#endif // MAINWINDOW_H

主窗口.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    toolBar= new QToolBar("Tool Bar");
    toolBar->addAction(QIcon(":/qt.png"), "FirstAction", this, SLOT(moveLeft()));
    toolBar->addAction(QIcon(":/qt.png"), "SecondAction", this, SLOT(moveRight()));
    addToolBar(Qt::RightToolBarArea, toolBar);
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::moveLeft()
{
    addToolBar(Qt::LeftToolBarArea, toolBar);
}
void MainWindow::moveRight()
{
    addToolBar(Qt::RightToolBarArea, toolBar);
}