在Qt设计师中创建自定义小部件以进行提升

8
我正在使用Qt开发GUI应用程序,我在将自定义小部件嵌入我的ui中遇到了一些困难。从Qt的文档中可以看出可以提升这样的小部件。然而,我仍然有些困惑应该如何完成这个过程。
我的小部件QTreeWidget受到Qt的torrent example的启发,在我的应用程序中我想要嵌入它:
因此,我的FilesView类(不包括源代码,因为它很简单):
#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设计师中实现这个功能?
1个回答

12
  • 在您希望放置FilesView的位置放置一个空小部件
  • 右键单击该小部件,选择Promote to
  • 提升类名设置为FilesView,按下Add,然后按下Promote
  • 您无法从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


谢谢,我确实尝试了这些步骤,但是我对如何处理委托感到困惑。很傻,我没有想到在main_window类中手动执行此操作。现在它可以工作了。附注:这是我在解释中链接的相同文档。 - aagaard

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接