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

aag*_*ard 6 qt widget

我正在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);
};
Run Code Online (Sandbox Code Playgroud)

这是一个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);
    }
};
Run Code Online (Sandbox Code Playgroud)

在示例中,将小部件嵌入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);
Run Code Online (Sandbox Code Playgroud)

我如何从Qt设计师那里做到这一点?

pne*_*zis 9

  • 将空小部件放在您想要的位置 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