Qt C ++创建工具栏 [英] Qt C++ Creating toolbar

查看:292
本文介绍了Qt C ++创建工具栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Qt并尝试一些例子在Foundations of Qt Development一书中。
在本书中,有一个教学单文档界面的部分,创建一个简单的应用程序,如记事本。
但是我在创建工具栏时遇到问题。

  MainWindow :: MainWindow(QWidget * parent):
QMainWindow(parent),
ui(new Ui :: MainWindow)
{
ui-> setupUi(this);
setAttribute(Qt :: WA_DeleteOnClose);
setWindowTitle(QString(%1 [*] - %2)arg(unnamed)。arg(SDI));

connect(ui-> docWidget-> document(),SIGNAL(modificationChanged(bool)),this,SLOT(setWindowModified(bool)));

createActions();
createMenu();
createToolbars();

statusBar() - > showMessage(Done);
}

这是主窗口的构造函数。

  void MainWindow :: createToolbars()
{
QToolBar * toolbar;
toolbar = addToolBar(tr(File));
toolbar-> addAction(anyaction);
}

这是本书创建工具栏的方式。
然而,当我尝试运行程序时,有两个工具栏创建。
一个是由代码创建的工具栏,称为File
另一个是由ui设计器创建的空白工具栏。 * ui.toolbar



为了摆脱两个工具栏,我试着只使用 * ui.toolbar
它的工作。代码如下所示。

  void MainWindow :: createToolbars()
{
ui- toolBar-> addAction(anyaction);
}

但是我试图通过代码创建工具栏。没有在ui设计器中添加工具栏。
所以我写这个:

  void MainWindow :: createToolbars()
{
QToolBar * FileBar = this-> addToolBar(tr(File));
FileBar-> addAction(anyaction);
}

但是,存在编译错误。
编译器使用此函数:

  void QMainWindow :: addToolBar(QT :: ToolBarArea area,QToolBar * toolbar) 

而不是我想要的:

  QToolBar * QMainWindow :: addToolBar(const QString& title)

http://doc.qt.io/qt-5/qmainwindow。 html#addToolBar-3



这里的错误是什么?

解决方案<当你从中删除​​ QToolBar 时,MainWindow QtCreator自动删除导入 QToolBar class。



只需将它添加到 mainwindow.h 的顶部:

  #include< QToolBar> 

最好定义 QToolBar * FileBar mainwindow.h 中的 MainWindow 部分中的 private 然后,您就可以使用 MainWindow 类的任何方法访问它。

  void MainWindow :: createToolbars()
{
FileBar = this-> addToolBar(tr(File));
FileBar-> addAction(anyaction);
}

当您看到此类消息时:


必须指向类/ struct / union /泛型类型


包含必要类的标题。


I am learning Qt and trying some examples in the book "Foundations of Qt Development". In the book, there is a section teaching Single Document Interface with an example creating a simple app like a notepad. However I am having problem with toolbar creating.

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_DeleteOnClose);
    setWindowTitle(QString("%1[*] - %2").arg("unnamed").arg("SDI"));

    connect(ui->docWidget->document(), SIGNAL(modificationChanged(bool)), this, SLOT(setWindowModified(bool)));

    createActions();
    createMenu();
    createToolbars();

    statusBar()->showMessage("Done");    
}

It is the constructor of the main window.

void MainWindow::createToolbars()
{
    QToolBar* toolbar;
    toolbar = addToolBar(tr("File"));
    toolbar->addAction(anyaction);
}

This is how the book create the toolbar. However, when I try to run the program, there are two toolbars created. One is the toolbar created by the code and called "File" Another is a blank toolbar created by the ui designer ie. *ui.toolbar.

In order to get rid of two toolbars, I tried using only the *ui.toolbar. It's working. The code is shown below.

void MainWindow::createToolbars()
{
    ui->toolBar->addAction(anyaction);
}

But I tried to create the toolbar by code only, ie. not adding a toolbar in the ui designer. So I write this:

void MainWindow::createToolbars()
{
    QToolBar* FileBar = this->addToolBar(tr("File"));
    FileBar->addAction(anyaction);
}

However, there is a compile error. The compiler use this function:

void QMainWindow::addToolBar(QT::ToolBarArea area, QToolBar * toolbar)

instead of what I want:

QToolBar * QMainWindow::addToolBar(const QString & title)

http://doc.qt.io/qt-5/qmainwindow.html#addToolBar-3

What is my mistake here?

解决方案

When you removed QToolBar from MainWindow QtCreator automatically removed import of QToolBar class.

Just add this to the top of mainwindow.h:

#include <QToolBar>

And it is better to define QToolBar* FileBar in private section of MainWindow in mainwindow.h. Then you will be able to access it from any method of MainWindow class.

void MainWindow::createToolbars()
{
    FileBar = this->addToolBar(tr("File"));
    FileBar->addAction(anyaction);
}

When you see such message:

must point to class/struct/union/generic type

First of all try to include headers for necessary class.

这篇关于Qt C ++创建工具栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆