I'm trying to make a folder selection dialog which looks like this:
The images come from this thread: can the Open File dialog be used to select a Folder?
I tried both QFileDialog::getExistingDirectory() and making an instance of QFileDialog and setting the properties. It just shows the Open File dialog with hidden files.
Try this one: QFileDialog::getExistingDirectory()
http://qt-project.org/doc/qt-4.8/qfiledialog.html#getExistingDirectory
http://doc.qt.io/qt-5/qfiledialog.html#getExistingDirectory
Both static methods open File selector in directory selection mode. If you don't like the look-n-feel of the opened dialog, you'll need to implement your own. By default Qt tries to open native one if possible, which should work in 99.9% of cases.
It seems that I have to answer myself.
I did it purely in Qt in the end, by making the entire dialog:
Header file:
#pragma once
#include <QtWidgets/QDialog>
class QTreeView;
class QFileSystemModel;
class QLineEdit;
class QPushButton;
class CDirSelectionDlg : public QDialog {
Q_OBJECT
public:
CDirSelectionDlg(const QString initialPath, QWidget *parent = nullptr);
QDir directory() const;
private:
void onCurrentChanged();
QTreeView *m_treeView;
QFileSystemModel *m_model;
QLineEdit *m_folderName;
QPushButton *m_OKbutton;
QString m_initialPath;
};
Source file:
#include "DirSelectionDlg.h"
#include <QLabel>
#include <QBoxLayout>
#include <QDialogButtonBox>
#include <QTreeView>
#include <QFileSystemModel>
#include <QPushButton>
#include <QLineEdit>
CDirSelectionDlg::CDirSelectionDlg(const QString initialPath, QWidget *parent) : QDialog(parent), m_initialPath(initialPath)
{
setMinimumSize(200, 300);
resize(400, 430);
m_model = new QFileSystemModel(this);
auto rootIdx = m_model->setRootPath(m_initialPath);
m_treeView = new QTreeView(this);
m_treeView->setModel(m_model);
m_treeView->setSelectionMode(QAbstractItemView::SingleSelection);
m_treeView->setHeaderHidden(true);
m_treeView->setSortingEnabled(true);
m_treeView->sortByColumn(0, Qt::AscendingOrder);
for(int i = 1; i < m_model->columnCount(); i ++) // don't show Size, Type, etc.
m_treeView->setColumnHidden(i, true);
m_treeView->scrollTo(rootIdx);
m_treeView->selectionModel()->setCurrentIndex(rootIdx, QItemSelectionModel::Current | QItemSelectionModel::Select);
connect(m_treeView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &CDirSelectionDlg::onCurrentChanged);
auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(buttonBox, &QDialogButtonBox::accepted, this, &CDirSelectionDlg::accept);
connect(buttonBox, &QDialogButtonBox::rejected, this, &CDirSelectionDlg::reject);
m_OKbutton = buttonBox->button(QDialogButtonBox::Ok);
auto label = new QLabel(tr("Folder:"));
m_folderName = new QLineEdit(this);
m_folderName->setReadOnly(true);
m_folderName->setText(QFileInfo(m_initialPath).fileName());
auto pathLayout = new QHBoxLayout();
pathLayout->addWidget(label);
pathLayout->addSpacing(10);
pathLayout->addWidget(m_folderName);
auto mainLayout = new QVBoxLayout();
mainLayout->addWidget(m_treeView);
mainLayout->addSpacing(10);
mainLayout->addLayout(pathLayout);
mainLayout->addSpacing(10);
mainLayout->addWidget(buttonBox);
setLayout(mainLayout);
}
void CDirSelectionDlg::onCurrentChanged()
{
auto fileInfo = m_model->fileInfo(m_treeView->selectionModel()->currentIndex());
m_folderName->setText(fileInfo.fileName());
m_OKbutton->setEnabled(fileInfo.isDir());
m_OKbutton->setDefault(fileInfo.isDir());
}
QDir CDirSelectionDlg::directory() const
{
return QDir(m_model->fileInfo(m_treeView->selectionModel()->currentIndex()).absoluteFilePath());
}
I'm using Qt 5.3.1.
It's 7 years later, but I had the same need, in this case using Python 3.8 and Qt 5.13.2, so I translated Andrej's code for PySide2. I figured this might be useful for someone else having this problem.
Even though it is not a native Windows widget, I think it looks decent on both Windows and Linux. I decided to change the type of the return value of directory() and the initial_path parameter to pathlib.Path for my application.
class DirectorySelectionDialog(QDialog):
def __init__(self, initial_path: Path, parent: QWidget) -> None:
super().__init__(parent=parent)
self.initial_path = initial_path
self.setMinimumSize(200, 300)
self.resize(400, 430)
self.model = QFileSystemModel(parent=self)
self.model.setFilter(QDir.AllDirs | QDir.NoDotAndDotDot)
root_index = self.model.setRootPath(str(self.initial_path))
self.tree_view = QTreeView(parent=self)
self.tree_view.setModel(self.model)
self.tree_view.setSelectionMode(QAbstractItemView.SingleSelection)
self.tree_view.setHeaderHidden(True)
self.tree_view.setSortingEnabled(True)
self.tree_view.sortByColumn(0, Qt.AscendingOrder)
for i in range(1, self.model.columnCount()):
self.tree_view.setColumnHidden(i, True)
self.tree_view.scrollTo(root_index)
self.tree_view.selectionModel().setCurrentIndex(root_index, QItemSelectionModel.Current | QItemSelectionModel.Select)
self.tree_view.selectionModel().selectionChanged.connect(self.onCurrentChanged)
button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
button_box.accepted.connect(self.accept)
button_box.rejected.connect(self.reject)
self.ok_button = button_box.button(QDialogButtonBox.Ok)
label = QLabel("Folder:")
self.folder_name = QLineEdit(parent=self)
self.folder_name.setReadOnly(True)
self.folder_name.setText(self.initial_path.name)
path_layout = QHBoxLayout()
path_layout.addWidget(label)
path_layout.addSpacing(10)
path_layout.addWidget(self.folder_name)
main_layout = QVBoxLayout()
main_layout.addWidget(self.tree_view)
main_layout.addSpacing(10)
main_layout.addLayout(path_layout)
main_layout.addSpacing(10)
main_layout.addWidget(button_box)
self.setLayout(main_layout)
def onCurrentChanged(self):
file_info = self.model.fileInfo(self.tree_view.selectionModel().currentIndex())
self.folder_name.setText(file_info.fileName())
self.ok_button.setEnabled(file_info.isDir())
self.ok_button.setDefault(file_info.isDir())
def directory(self) -> Path:
file_info = self.model.fileInfo(self.tree_view.selectionModel().currentIndex())
return Path(file_info.absoluteFilePath())
Related
In one of my projects on Qt, I have this requirement of selecting multiple items from a folder like view (tree-view), and populate the selected items in another widget. To display available items for multi-selection, I'm using a QTreeView, and populating element hierarchy like the following
m_StandardModel = new QStandardItemModel ;
QStandardItem *rootNode = m_StandardModel->invisibleRootItem();
//defining a couple of items
QStandardItem *item1 = new QStandardItem(tr("ITEM1"));
QStandardItem *item2 = new QStandardItem(tr("ITEM2"));
QStandardItem *item3 = new QStandardItem(tr("ITEM3"));
QStandardItem *item4 = new QStandardItem(tr("ITEM4"));
rootNode->appendRow(item1 );
rootNode->appendRow(item2 );
rootNode->appendRow(item3 );
rootNode->appendRow(item4 );
//register the model
ui->treeView->setModel(m_StandardModel);
ui->treeView->expandAll();
//enabling multiselection behaviour
QItemSelectionModel *selectionModel= ui->treeView->selectionModel();
ui->treeView->setSelectionMode(QAbstractItemView::MultiSelection);
Everything's fine till here. I'm able to display my items in tree-view and also able to multiselect items. The problem occurs when I'm trying to use these multiple selected items from tree view.
In my UI, I have connected a button's clicked() signal to my slot, which handles the iteration and manipulation of selected items. Here's the function that's being called:
//User selects a number of features listed on the left pane and clicks this button to disable them
void MainWindow::on_pushButton_2_clicked()
{
QModelIndexList selectedItems = ui->treeView->selectionModel()->selectedIndexes();
QStringList items;
foreach(QModelIndex index, selectedItems)
{
QStandardItemModel* itemModel = dynamic_cast<QStandardItemModel*>(ui->treeView->model());
if(itemModel)
{
QStandardItem* item = itemModel->itemFromIndex(index);
items<< item->data().toString();
}
}
}
Debugging till the function end is perfect. But as soon as I exit this function (shown above), I get a DEBUG ASSERTION !! like the following
HEAP[myprog.exe]: Invalid address specified to RtlValidateHeap( 00390000, 01946798 )
The call stack is showing that this assertion is reached because of destruction of local QModelIndexList that I've created in the function.
Following is the call stack at the time of debug assertion:
Any idea, what I might be missing? I have tried a number of times, but yet unable to figure out the real problem. Is there a better way of doing what is being done here?
I'm using QT 4.8.4, and am building/debugging my application in DEBUG configuration on Windows 7.
I tried your code in a Windows application and I don't see your behaviour, maybe you have extra code in other part of the program that generates that behaviour. The code I tested is the following (I only modified the way to get the data, ot get the display role item->data(Qt::DisplayRole).toString()):
The cpp file:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_StandardModel = new QStandardItemModel ;
QStandardItem *rootNode = m_StandardModel->invisibleRootItem();
//defining a couple of items
QStandardItem *item1 = new QStandardItem(tr("ITEM1"));
QStandardItem *item2 = new QStandardItem(tr("ITEM2"));
QStandardItem *item3 = new QStandardItem(tr("ITEM3"));
QStandardItem *item4 = new QStandardItem(tr("ITEM4"));
rootNode->appendRow(item1 );
rootNode->appendRow(item2 );
rootNode->appendRow(item3 );
rootNode->appendRow(item4 );
//register the model
ui->treeView->setModel(m_StandardModel);
ui->treeView->expandAll();
//enabling multiselection behaviour
QItemSelectionModel *selectionModel= ui->treeView->selectionModel();
ui->treeView->setSelectionMode(QAbstractItemView::MultiSelection);
connect(ui->pushButton, SIGNAL(clicked()),
this, SLOT(on_pushButton_clicked()) );
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked(){
QModelIndexList selectedItems = ui->treeView->selectionModel()->selectedIndexes();
QStringList items;
foreach(QModelIndex index, selectedItems)
{
QStandardItemModel* itemModel = dynamic_cast<QStandardItemModel*>(ui->treeView->model());
if(itemModel)
{
QStandardItem* item = itemModel->itemFromIndex(index);
items << item->data(Qt::DisplayRole).toString();
}
}
}
The header file:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QStandardItemModel>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
QStandardItemModel * m_StandardModel ;
};
#endif // MAINWINDOW_H
Maybe you can publish the hole source code to see the behaviour you are having.
I am trying to get the list of widgets from a .ui files.
So here is a bit of code:
QUiLoader loader;
QFile file(fname);
file.open(QFile::ReadOnly);
QWidget *myWidget = loader.load(&file, this);
file.close();
QStringList avlb_wd = loader.availableWidgets();
QMessageBox msg;
foreach (const QString &str, avlb_wd)
{
msg.setText(str);
msg.exec();
}
But as I can see, availableWidgets() gives me all the widgets, not the ones that are in .ui file.
How can I achieve it?
Thanks forward.
Make a subclass of QUiLoader, and reimplement createWidget, createLayout and createAction (there's also createActionGroup, but it's not really supported any more, unless you manually edit the ui file).
These functions are called every time a new widget, layout or action is created by the ui loader. So just call the base-class implementation to get the created object, and then you can gather whatever information you like, before returning it.
UPDATE:
So the basic QUiLoader subclass would look like this (add other overloads as required):
class UiLoader : public QUiLoader
{
Q_OBJECT
public:
UiLoader(QObject *parent = 0) : QUiLoader(parent) { }
virtual QWidget* createWidget(const QString &className,
QWidget *parent = 0, const QString &name = QString())
{
QWidget* widget = QUiLoader::createWidget(className, parent, name);
// do stuff with className, name, widget, etc
return widget;
}
};
I have built an app in Qt that contains two buttons: an exit button and an import button. When the import button is pushed, a list of buttons is shown in a scrollarea on the screen (the file loggers.csv contains the data 1;2;3;4;5;).
It all works fine, but when I push the exit button (which of course should close everything), the app is not stopped properly (the stop button of Qt is still active, and the play button isn't). When I run the debugger and push the exit button it gives an error: Invalid address specified to RtlFreeHeap( 0ADF0000, 0028FE40 ). Can anybody help me?
main
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.showFullScreen();
return a.exec();
}
Mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtGui>
#include "logger.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QPushButton exit_btn;
QPushButton import_btn;
private slots:
void createMenus();
void exit();
void import();
private:
int window_width;
int window_height;
int numLoggers;
int numSelected;
QVector<Logger*> loggers;
QScrollArea * scroll_area;
QVBoxLayout scrollLayout;
QWidget viewport;
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
Mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QtGui"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
window_width = QApplication::desktop()->width();
window_height = QApplication::desktop()->height();
createMenus();
connect(&exit_btn,SIGNAL(clicked()),this,SLOT(exit()));
connect(&import_btn,SIGNAL(clicked()),this,SLOT(import()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::createMenus()
{
import_btn.setParent(ui->centralWidget);
import_btn.setGeometry(400,300,100,100);
import_btn.setText("IMPORT");
exit_btn.setText("EXIT");
exit_btn.setParent(ui->centralWidget);
exit_btn.setGeometry(window_width-50,12,32,32);
viewport.setLayout(&scrollLayout);
viewport.resize(0,0);
scroll_area = new QScrollArea(ui->centralWidget);
scroll_area->setGeometry(0,66,317,window_height-116);
scroll_area->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
scroll_area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
scroll_area->setWidget(&viewport);
scroll_area->setGeometry(0,97,317,window_height-228);
scrollLayout.setMargin(0);
scrollLayout.setSpacing(0);
}
void MainWindow::exit()
{
close();
qApp->quit();
}
void MainWindow::import()
{
numSelected=0;
QFile f("Loggers3.csv");
if (f.open(QIODevice::ReadOnly))
{
numLoggers=0;
QString data;
data = f.readAll();
QStringList vals = data.split(';');
while(vals.size()>=1)
{
Logger * logger = new Logger;
logger->setNumber(vals[0].toInt());
vals.removeAt(0);
loggers<<logger;
numLoggers++;
}
f.close();
for(int i=0; i<numLoggers;i++)
{
loggers[i]->createButtons();
scrollLayout.addWidget(loggers[i]->button);
}
viewport.resize(367,numLoggers*60);
}
}
logger.h
#ifndef LOGGER_H
#define LOGGER_H
#include <QtGui>
class Logger : public QWidget
{
Q_OBJECT
public:
explicit Logger(QWidget *parent = 0);
~Logger();
int number;
QLabel num;
QToolButton * button;
bool checked;
signals:
public slots:
void setNumber(int number);
void createButtons();
};
#endif // LOGGER_H
logger.cpp
#include "logger.h"
#include <QtGui>
Logger::Logger(QWidget *parent) :
QWidget(parent)
{
button = new QToolButton;
button->setCheckable(true);
button->setMinimumSize(317,60);
button->setStyleSheet("QToolButton{background-image: url(images/btn_bg); border:none}");
}
Logger::~Logger()
{
}
void Logger::setNumber(int logNumber)
{
number=logNumber;
}
void Logger::createButtons()
{
QLayout * layout = new QHBoxLayout;
QSpacerItem *spacer = new QSpacerItem(120, 31, QSizePolicy::Maximum, SizePolicy::Maximum);
num.setStyleSheet("color: white; font: bold 16px");
num.setText(QString::number(number));
layout->addWidget(&num);
layout->addItem(spacer);
button->setLayout(layout);
}
I'm not entirely certain about what you are trying to achieve... but your problem lies with these two lines:
viewport.setLayout(&scrollLayout);
viewport.resize(0,0);
In the documentation for the QWidget class it states that:
If there already is a layout manager installed on this widget, QWidget
won't let you install another. You must first delete the existing layout manager (returned by layout()) before you can call setLayout() with the new layout.
This is where your problem lies. Don't believe me, add this check before those two lines of code.
if(layout()){
qDebug() << "Another layout exists";
}
Source: QVBoxLayout Class Reference
The QVBoxLayout class lines up widgets vertically.
This class is used to construct vertical box layout objects. See QBoxLayout for details.
The simplest use of the class is like this:
QWidget *window = new QWidget;
QPushButton *button1 = new QPushButton("One");
QPushButton *button2 = new QPushButton("Two");
QPushButton *button3 = new QPushButton("Three");
QPushButton *button4 = new QPushButton("Four");
QPushButton *button5 = new QPushButton("Five");
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(button1);
layout->addWidget(button2);
layout->addWidget(button3);
layout->addWidget(button4);
layout->addWidget(button5);
window->setLayout(layout);
window->show();
First, we create the widgets we want in the layout. Then, we create the QVBoxLayout object and add the widgets into the layout. Finally, we call QWidget::setLayout() to install the QVBoxLayout object onto the widget. At that point, the widgets in the layout are reparented to have window as their parent.
Critical source of error in your project:
Widgets should be constructed on the heap because they will be deleted automatically when their parents are deleted. You have a custom widget class that you instantiate on the heap. The members should also go on the heap. Also, you should consider using the parent /child hierarchy in your GUI code to ensure proper memory management and proper deletion.
In my experience, if your program stops in RtlFreeHeap it is a good sign of memory corruption.
When calling
import_btn.setParent(ui->centralWidget);
centralWidget takes ownership of import_btn. That means, when centralWidget is deleted (which happens as part of delete ui;in your MainWindow's destructor), it will call delete on your member variable!
This leads to the reported memory corruption.
You need to allocate your QPushButton's dynamically, not as a plain member variable. So make them QPushButton*.
Here's how I did it from mainwindow.cpp, thanks to and this question: How to create a correct exit button in qt
QPushButton * quit_btn = new QPushButton(this);
quit_btn->setGeometry(540,440,93,27);
quit_btn->setText("Exit");
QObject::connect(quit_btn,SIGNAL(clicked()),qApp,SLOT(quit()));
Works flawlessly :D
I'm trying to create a menu in Qt following this example http://doc.qt.nokia.com/latest/mainwindows-menus.html
but I keep getting the error 'menuBar' not declared in this scope
void Window::createMenus()
{
saveMenu = menuBar()->addMenu("&Save");
}
In context:
#include <QtGui>
#include "borderlayout.h"
#include "window.h"
Window::Window()
{
QTextBrowser *centralWidget = new QTextBrowser;
//***Change this to whatever widget(s) the drawing area is. QPainter or something?
centralWidget->setPlainText(tr("DRAW HERE YAY"));
BorderLayout *layout = new BorderLayout;
layout->addWidget(centralWidget, BorderLayout::Center);
layout->addWidget(createLabel("File ..."), BorderLayout::North);
layout->addWidget(createLabel("Toolbar yo!"), BorderLayout::West);
//layout->addWidget(createLabel("Status bar"), BorderLayout::South);
//Maybe we could put in a status bar. For now let's not worry about it. It's not a requirement.
setLayout(layout);
createMenus();
setWindowTitle(tr("Border Layout"));
}
QLabel *Window::createLabel(const QString &text)
{
QLabel *label = new QLabel(text);
label->setFrameStyle(QFrame::Box | QFrame::Raised);
return label;
}
void Window::createMenus()
{
saveMenu = menuBar()->addMenu("&Save");
}
window.h
#ifndef WINDOW_H
#define WINDOW_H
#include <QWidget>
class QLabel;
class QMenu;
class Window : public QWidget
{
Q_OBJECT
public:
Window();
private:
void createMenus();
QLabel *createLabel(const QString &text);
QMenu *saveMenu();
};
#endif
window.cpp
#include <QtGui>
#include "borderlayout.h"
#include "window.h"
Window::Window()
{
QTextBrowser *centralWidget = new QTextBrowser;
//***Change this to whatever widget(s) the drawing area is. QPainter or something?
centralWidget->setPlainText(tr("DRAW HERE YAY"));
BorderLayout *layout = new BorderLayout;
layout->addWidget(centralWidget, BorderLayout::Center);
layout->addWidget(createLabel("File ..."), BorderLayout::North);
layout->addWidget(createLabel("Toolbar yo!"), BorderLayout::West);
//layout->addWidget(createLabel("Status bar"), BorderLayout::South);
//Maybe we could put in a status bar. For now let's not worry about it. It's not a requirement.
setLayout(layout);
createMenus();
setWindowTitle(tr("Border Layout"));
}
QLabel *Window::createLabel(const QString &text)
{
QLabel *label = new QLabel(text);
label->setFrameStyle(QFrame::Box | QFrame::Raised);
return label;
}
void Window::createMenus()
{
saveMenu = menuBar()->addMenu("&Save");
}
The menu bar is a feature of the QMainWindow class.
Because your Window class is being inherited directly from QWidget, it does not have the menuBar method, hence your error.
You need to subclass your Window class from QMainWindow rather than QWidget.
I am developing a GUI application in Qt, and I have some difficulties embedding a custom widget in my ui. From Qt's documentation I can see it is possible to promote such a widget. However, I am still a little confused about how this should be done.
My widget QTreeWidget is heavily inspired by Qt's torrent example, where I want to embed this in my application:
So I have for my FilesView class (not included the src code, 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);
};
To this is a TorrentViewDelegate class (comment the progressbar for testing purposes)
#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);
}
};
In the example the embed the widget in 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 the Qt designer?
Place an empty widget where you want to have your FilesView
Right click on it and select Promote to
Set the promoted class name to FilesView press Add and then Promote
You cannot set the delegate from QtDesigner
For more info have a look here:
http://qt-project.org/doc/qt-4.8/designer-using-custom-widgets.html
The second option you have is to create a plugin for your widget which will allow you to set its properties through designer. If you are not going to use your widget multiple times I do not suggest it. For more details check the following link:
http://qt-project.org/doc/qt-4.8/designer-creating-custom-widgets.html