繁体   English   中英

创建要在Qt设计器中升级的自定义窗口小部件

[英]Creating custom widget to be promoted in Qt designer

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

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

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

#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类(注释进度条用于测试目的)

#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中:

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设计师那里做到这一点?

  • 将空小部件放在要使用FilesView
  • 右键单击它并选择Promote to
  • 提升的类名设置为FilesView按“ 添加” ,然后按“ 提升”
  • 您无法从QtDesigner设置委托

有关详细信息,请查看此处:

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

您拥有的第二个选项是为您的小部件创建一个插件,允许您通过设计器设置其属性。 如果您不打算多次使用您的小部件,我不建议它。 有关详细信息,请查看以下链接:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM