I am developing a GUI application in Qt and I have some difficulties embedding a custom widget in my ui. From the Qt documentation, I see that you can promote such a widget. However, I am still a little confused about how this should be done.
My QTreeWidget widget is heavily inspired by the Qt torrent example , where I want to embed it in my application:
So, I have a FilesView class (src code is not included because it is trivial):
#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); };
This is the TorrentViewDelegate class (comment on the progress bar for testing purposes)
#include <QItemDelegate>
In the example, insert the widget into MainWindow as:
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);
How can I do this from a Qt designer?
source share