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