Я разрабатываю приложение с графическим интерфейсом пользователя на Qt, и у меня есть некоторые трудности со встраиванием пользовательского виджета в свой пользовательский интерфейс. Из документации Qt я вижу, что можно продвигать такой виджет. Однако я все еще немного не понимаю, как это нужно делать.
Мой виджет QTreeWidget в значительной степени вдохновлен примером торрента Qt , где я хочу встроить это в свое приложение:
Итак, у меня есть класс FilesView (не включен класс src, потому что он тривиален):
#include
#include
#include
#include
#include
// 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
#include
#include
// 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(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?