在 Qt 设计器中创建要提升的自定义小部件 [英] Creating custom widget to be promoted in Qt designer

查看:14
本文介绍了在 Qt 设计器中创建要提升的自定义小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Qt 开发一个 GUI 应用程序,但在我的 ui 中嵌入自定义小部件时遇到了一些困难.从 Qt 的 documentation 我可以看到有可能推广这样的小部件.但是,我仍然对如何做到这一点感到有些困惑.

I am developing a GUI application in Qt, and I have some difficulties embedding a custom widget in my ui. From Qt's documentation I can see it is possible to promote such a widget. However, I am still a little confused about how this should be done.

我的小部件 QTreeWidget 深受 Qt torrent 示例的启发,其中我想将其嵌入到我的应用程序中:

My widget QTreeWidget is heavily inspired by Qt's torrent example, where I want to embed this in my application:

所以我有我的 FilesView 类(不包括 src 代码,因为它是微不足道的):

So I have for my FilesView class (not included the src code, because it is trivial):

#include <QTreeWidget>
#include <QUrl>
#include <QFile>
#include <QDragMoveEvent>
#include <QDropEvent>

// FilesView extends QTreeWidget to allow drag and drop.
class FilesView : public QTreeWidget
{
    Q_OBJECT
public:
    FilesView(QWidget *parent = 0);

signals:
    void fileDropped(const QString &fileName);

protected:
    void dragMoveEvent(QDragMoveEvent *event);
    void dropEvent(QDropEvent *event);
};

这是一个 TorrentViewDelegate 类(评论进度条以进行测试)

To this is a TorrentViewDelegate class (comment the progressbar for testing purposes)

#include <QItemDelegate>
#include <QMainWindow>
#include <QApplication>

// TorrentViewDelegate is used to draw the progress bars.
class TorrentViewDelegate : public QItemDelegate
{
    Q_OBJECT
public:
    inline TorrentViewDelegate(QMainWindow *mainWindow) : QItemDelegate(mainWindow) {}

    inline void paint(QPainter *painter, const QStyleOptionViewItem &option,
                      const QModelIndex &index ) const
    {
        if (index.column() != 2) {
            QItemDelegate::paint(painter, option, index);
            return;
        }

        // Set up a QStyleOptionProgressBar to precisely mimic the
        // environment of a progress bar.
        QStyleOptionProgressBar progressBarOption;
        progressBarOption.state = QStyle::State_Enabled;
        progressBarOption.direction = QApplication::layoutDirection();
        progressBarOption.rect = option.rect;
        progressBarOption.fontMetrics = QApplication::fontMetrics();
        progressBarOption.minimum = 0;
        progressBarOption.maximum = 100;
        progressBarOption.textAlignment = Qt::AlignCenter;
        progressBarOption.textVisible = true;

        // Set the progress and text values of the style option.
        //int progress = qobject_cast<MainWindow *>(parent())->clientForRow(index.row())->progress();
        int progress = 40;
        progressBarOption.progress = progress < 0 ? 0 : progress;
        progressBarOption.text = QString().sprintf("%d%%", progressBarOption.progress);

        // Draw the progress bar onto the view.
        QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter);
    }
};

在示例中,将小部件嵌入 MainWindow 为:

In the example the embed the widget in MainWindow as:

filesView = new FilesView(this);
filesView->setItemDelegate(new TorrentViewDelegate(this));
filesView->setHeaderLabels(headers);
filesView->setSelectionBehavior(QAbstractItemView::SelectRows);
filesView->setAlternatingRowColors(true);
filesView->setRootIsDecorated(false);
ui->verticalLayout_Filebox->addWidget(filesView);

如何通过 Qt 设计器做到这一点?

How can I do this from the Qt designer?

推荐答案

  • 在您想要放置 FilesView
  • 的位置放置一个空小部件
  • 右键单击它并选择推广到
  • 提升的类名设置为FilesViewAdd然后Promote
  • 您不能从 QtDesigner 设置委托
  • 更多信息请看这里:

    http://qt-project.org/doc/qt-4.8/designer-using-custom-widgets.html

    您的第二个选项是为您的小部件创建一个插件,允许您通过设计器设置其属性.如果您不打算多次使用您的小部件,我不建议这样做.更多详情请查看以下链接:

    The second option you have is to create a plugin for your widget which will allow you to set its properties through designer. If you are not going to use your widget multiple times I do not suggest it. For more details check the following link:

    http://qt-project.org/doc/qt-4.8/designer-creating-custom-widgets.html

    这篇关于在 Qt 设计器中创建要提升的自定义小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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