Set a QStandartItem as Expandable without having a child item - c++

i am trying to send a folder structure between two differnt programs which can be on different computers. On my server I have a QFileSystemModel and on my client I have a QTreeView which has a QStandardItemModel as a model. And i have a prebuild signal/slot system which can send QString and QStringList between the programs.
Client:
auto *m_stdItemModel = new QStandardItemModel(this);
auto *m_treeView = new QTreeView(this);
m_treeView->setModel(m_stdItemModel);
I want to sent the child directories from the server everytime I click on the expand button on the client m_treeView. The problem is that an entry is only expandable when it has a children.
My approach to do so was adding a dummy child and removing it when the user clicks the expand button.
Adding dummy:
void addChildToParent(QString child, QStandardItem *parentItem, bool isExpandable)
{
auto *childItem = new QStandardItem(child);
if(isExpandable)
{
childItem->appendRow(new QStandardItem("dummy"));
}
parentItem->appendRow(childItem);
}
Is there a workaround to do add an expand button without adding a dummy child?
Kind regards

I think your best bet would be to override QStandardItemModel::hasChildren...
class item_model: public QStandardItemModel {
using super = QStandardItemModel;
public:
virtual bool hasChildren (const QModelIndex &parent = QModelIndex()) const override
{
if (const auto *item = itemFromIndex(parent)) {
/*
* Here you need to return true or false depending on whether
* or not any attached views should treat `item' as having one
* or more child items. Presumably based on the same logic
* that governs `isExpandable' in the code you've shown.
*/
return is_expandable(item);
}
return super::hasChildren(parent);
}
};

Related

How to get selected items from a QListView which uses custom QAbstractListModel in C++

I have created a custom list model following this guide Creating a cusotm model for a QListView. I am able to show a list of custom objects (such as the Employee as in the example) but I don't know how to retrieve back the selected ones (can I retrieve back the "linked" objects directly?).
Maybe do I have to do something with this command:
myLV->selectionModel()->selectedIndexes();
But I don't really know how to retrieve back the original custom objects.
[EDIT]
So far I have solved retrieving back the object adding a custom method inside my custom list model:
Employee* MyEmployeeListModel::getAtSelectedIndex(const QModelIndex& index){
return employees_.at(index.row());
}
And then calling this on the main window:
QModelIndexList selectedRows;
QItemSelectionModel * selmodel = ui->employeesLV->selectionModel();
selectedRows = selmodel->selectedRows();
MyEmployeeListModel* currModel = dynamic_cast <MyEmployeeListModel*>(ui->employeesLV->model());
for (const QModelIndex & index : selectedRows){
Employee* item=currModel->getAtSelectedIndex(index);
if (item) {
// do something with the item
}
}
Now what I am willing to know is if this is the real best practice or not.
I'm using the following code with a QTreeView (ui->treeMessages), but this should work with a QListView as well:
QModelIndexList selectedRows;
QItemSelectionModel * selmodel = ui->treeMessages->selectionModel();
selectedRows = selmodel->selectedRows();
for (const QModelIndex & index : selectedRows)
{
const QModelIndex sourceIndex = m_sortFilterModel->mapToSource(index);
ItemData * item = sourceIndex.internalPointer();
if (item) {
// do something
}
}

Hide items of a QFileSystemModel/QTreeView via indexes using a specific rule

I'm displaying the contents of a folder in my Qt program using a QTreeView + QFileSystemModel.
Now I want to hide specific items of that view. The display rule is not based on the file names, so I can't use setNameFilters(). What I have is a simple list of QModelIndex containing all the items I want to hide. Is there a way of filtering the view using only this list?
In my research I came across the QSortFilterProxyModel class, but I couldn't figure how to use it in order to achieve what I want. Any help would be appreciated.
Subclass QSortFilterProxyModel and override the method filterAcceptsRow to set the filter logic.
For example, to filter on current user write permissions :
class PermissionsFilterProxy: public QSortFilterProxyModel
{
public:
PermissionsFilterProxy(QObject* parent=nullptr): QSortFilterProxyModel(parent)
{}
bool filterAcceptsRow(int sourceRow,
const QModelIndex &sourceParent) const
{
QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
QFileDevice::Permissions permissions = static_cast<QFileDevice::Permissions>(index.data(QFileSystemModel::FilePermissions).toInt());
return permissions.testFlag(QFileDevice::WriteUser); // Ok if user can write
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QFileSystemModel* model = new QFileSystemModel();
model->setRootPath(".");
QTreeView* view = new QTreeView();
PermissionsFilterProxy* proxy = new PermissionsFilterProxy();
proxy->setSourceModel(model);
view->setModel(proxy);
view->show();
return app.exec();
}

Is it possible to call a slot when any of the widgets in a dialog box emits a signal?

I am trying to create a configuration menu box for an application, and have used a QDialog box to display the options that the user can change. This box contains QComboBoxes and QLineEdits, but a lot of them (7 combo boxes and 12 line edits). There is a QPushButton in the bottom called "Apply Changes" that should get enabled only when any property in the box gets changed.
Do I have to link every signal from each widget with a slot to enable the button individually or is there a signal that the QDialog box itself emits when there is a change in its constituent widgets?
Right now I have this:
connect(Combo1,SIGNAL(activated(QString)),this,SLOT(fnEnable(QString)));
connect(Combo2,SIGNAL(activated(QString)),this,SLOT(fnEnable(QString)))
followed by 17 more lines of these connections.
void MyClass::fnEnable(QString)
{
ApplyButton->setEnabled(true); //It is initialised as false
}
I was wondering if there was a shorter way of doing this, maybe (like I mentioned before) a signal emitted by QDialog (I couldn't find one in the documentation)
I know that this does not speed up the program, as only the required connection is called, but it would make any further attempts at making more ambitious dialog boxes easier.
Actually there is no such signal, but one approach is to create a list of QComboBox, and make the connections with a for, for example:
QList <*QCombobox> l;
l<<combobox1<< combobox2<< ....;
for (auto combo: l) {
connect(combo, &QComboBox::activated, this, &MyClass::fnEnable);
}
The same would be done with QLineEdit.
You can iterate over an initializer list of widgets boxes and leverage C++11 to do all the boring work for you:
MyClass::MyClass(QWidget * parent) : QWidget(parent) {
auto const comboBoxes = {Combo1, Combo2, ... };
for (auto combo : comboBoxes)
connect(combo, &QComboBox::activates, this, &MyClass::fnEnable);
}
You can also automatically find all the combo boxes:
MyClass::MyClass(QWidget * parent) : QWidget(parent) {
ui.setupUi(this); // or other setup code
for (auto combo : findChildren<QComboBox*>(this))
connect(combo, &QComboBox::activated, this, &MyClass::fnEnable);
}
Or you can automatically attach to the user property's change signal. This will work on all controls that have the user property. The user property is the property of a control that contains the primary data the control is displaying.
void for_layout_widgets(QLayout * layout, const std::function<void(QWidget*)> & fun,
const std::function<bool(QWidget*)> & pred = +[](QWidget*){ return true; })
{
if (!layout) return;
for (int i = 0; i < layout->count(); ++i) {
auto item = layout->itemAt(i);
for_layout_widgets(item->layout(), fun, pred);
auto widget = item->widget();
if (widget && pred(widget)) fun(widget);
}
}
class MyClass : public QWidget {
Q_OBJECT
Q_SLOT void MyClass::fnEnable(); // must take no arguments
...
};
MyClass::MyClass(QWidget * parent) : QWidget(parent) {
// setup code here
auto slot = metaObject()->method(metaObject()->indexOfMethod("fnEnable()"));
Q_ASSERT(slot.isValid());
for_layout_widgets(layout(), [=](QWidget * widget){
auto mo = widget->metaObject();
auto user = mo->userProperty();
if (!user.isValid()) return;
auto notify = user.notifySignal();
if (!notify.isValid()) return;
connect(widget, notify, this, slot);
});
}
You can also keep the combo boxes in an array, by value. This minimizes the costs of indirect references and results in code that will take the least amount of memory possible and perform well:
class MyClass : public QWidget {
Q_OBJECT
QVBoxLayout m_layout{this};
std::array<QComboBox, 14> m_comboBoxes;
...
};
MyClass(QWidget * parent) : QWidget(parent) {
for (auto & combo : m_comboBoxes) {
m_layout.addWidget(&combo);
connect(&combo, &QComboBox::activates, this, &MyClass::fnEnable);
}
}

QFileDialog: How to select multiple files with touch-screen input?

In our application that uses Qt 4 and supports touch input, we use the QFileDialog with the options QFileDialog::DontUseNativeDialog and QFileDialog::ExistingFiles.
The first is needed because we set our own stylesheet and that does not work with the native dialog. The second is for needed for selecting multiple files, which is what we want to do.
The problem ist that one can not select multiple files with touch input in the QFileDialog, because we have no "shift" or "ctrl"-key available. In Windows the problem is solved by adding checkboxes to the items. QFileDialog has no checkboxes.
I tried to manipulate the QFileDialog to make it displays check boxes for the items, but I failed.
I tried to exchanged the QFileSystemModel that is used by the underlying QTreeView and QListView, but this breaks the signal-slot connections between the model and the dialog. I could not find a way to restore them because they are burried deep in the private intestants of the dialog.
At this moment the only solution I can imagine is writing a whole new dialog, but I would like to avoid the effort.
So is there a way to add checkboxes to the QFileDialog model views ?
Do you have another idea how selecting multiple files could be made possible?
Is the problem fixed in Qt 5? We want to update anyway.
Thank you for your Help.
As I failed to add checkboxes to the item views, I implemented a "hacky" work-around. It adds an extra checkable button to the dialog that acts as a "ctrl"-key. When the button is checked, multiple files can be selected. The solution is a little bit ugly, because it relies on knowing the internals of the dialog, but it does the job.
So here is the code for the header ...
// file touchfiledialog.h
/*
Event filter that is used to add a shift modifier to left mouse button events.
This is used as a helper class for the TouchFileDialog
*/
class EventFilterCtrlModifier : public QObject
{
Q_OBJECT;
bool addCtrlModifier;
public:
EventFilterCtrlModifier( QObject* parent);
void setAddCtrlModifier(bool b);
protected:
virtual bool eventFilter( QObject* watched, QEvent* e);
};
/*
TouchDialog adds the possibility to select multiple files with touch input to the QFileDialog.
This is done by adding an extra button which can be used as control key replacement.
*/
class QTOOLS_API TouchFileDialog : public QFileDialog
{
Q_OBJECT
EventFilterCtrlModifier* listViewEventFilter;
EventFilterCtrlModifier* treeViewEventFilter;
bool initialized;
public:
TouchFileDialog( QWidget* parent);
protected:
virtual void showEvent( QShowEvent* e);
private slots:
void activateCtrlModifier(bool b);
private:
void initObjectsForMultipleFileSelection();
};
with the implementation ...
// file touchfiledialog.cpp
#include "touchfiledialog.h"
EventFilterCtrlModifier::EventFilterCtrlModifier(QObject* parent)
: QObject(parent)
, addCtrlModifier(false)
{
}
void EventFilterCtrlModifier::setAddCtrlModifier(bool b)
{
addCtrlModifier = b;
}
bool EventFilterCtrlModifier::eventFilter(QObject* watched, QEvent* e)
{
QEvent::Type type = e->type();
if( type == QEvent::MouseButtonPress || type == QEvent::MouseButtonRelease)
{
if( addCtrlModifier)
{
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(e);
// Create and post a new event with ctrl modifier if the event does not already have one.
if( !mouseEvent->modifiers().testFlag(Qt::ControlModifier))
{
QMouseEvent* newEventWithModifier = new QMouseEvent(
type,
mouseEvent->pos(),
mouseEvent->globalPos(),
mouseEvent->button(),
mouseEvent->buttons(),
mouseEvent->modifiers() | Qt::ControlModifier
);
QCoreApplication::postEvent(watched, newEventWithModifier);
return true; // absorb the original event
}
}
}
return false;
}
//#######################################################################################
TouchFileDialog::TouchFileDialog(QWidget* parent)
: QFileDialog(parent)
, listViewEventFilter(NULL)
, treeViewEventFilter(NULL)
, initialized(false)
{
}
void TouchFileDialog::showEvent(QShowEvent* e)
{
// install objects that are needed for multiple file selection if needed
if( !initialized)
{
if( fileMode() == QFileDialog::ExistingFiles)
{
initObjectsForMultipleFileSelection();
}
initialized = true;
}
QFileDialog::showEvent(e);
}
void TouchFileDialog::initObjectsForMultipleFileSelection()
{
// install event filter to item views that are used to add ctrl modifiers to mouse events
listViewEventFilter = new EventFilterCtrlModifier(this);
QListView* listView = findChild<QListView*>();
listView->viewport()->installEventFilter(listViewEventFilter);
treeViewEventFilter = new EventFilterCtrlModifier(this);
QTreeView* treeView = findChild<QTreeView*>();
treeView->viewport()->installEventFilter(treeViewEventFilter);
QGridLayout* dialogLayout = static_cast<QGridLayout*>(layout()); // Ugly because it makes assumptions about the internals of the QFileDialog
QPushButton* pushButtonSelectMultiple = new QPushButton(this);
pushButtonSelectMultiple->setText(tr("Select multiple"));
pushButtonSelectMultiple->setCheckable(true);
connect( pushButtonSelectMultiple, SIGNAL(toggled(bool)), this, SLOT(activateCtrlModifier(bool)));
dialogLayout->addWidget(pushButtonSelectMultiple, 2, 0);
}
void ZFFileDialog::activateCtrlModifier(bool b)
{
listViewEventFilter->setAddCtrlModifier(b);
treeViewEventFilter->setAddCtrlModifier(b);
}
The TouchFileDialog installs an event filter to the item views that will add a ControlModifier to the mouse events of the views when the corresponging button in the dialog is checked.
Feel free to post other solutions, because this is somewhat improvised.

Changing data in a QTableView depending on the selection of a QComboBox

I have a QComboBox in one of the columns of a QTableView. How can I change the other columns depending on what I selected in the ComboBox? I am using the QComboBox as a delegate.
There are at least 2 approaches.
Use natural for Qt's model itemChanged signal.
emit signal from your delegate and catch it inside your main window.
If your delegate is standard which means that inside setModelData() method you have something like:
QComboBox *line = static_cast<QComboBox*>(editor);
QString data = line->currentText();
//...
model->setData(index, data);
then I think you should use just natural way. For example:
connect(model,&QStandardItemModel::itemChanged,[=](QStandardItem * item) {
if(item->column() == NEEDED_COLUMN)
{
//you found, just get data and use it as you want
qDebug() << item->text();
}
});
I used here C++11 (CONFIG += c++11 to .pro file) and new syntax of signals and slots, but of course you can use old syntax if you want.
I already reproduced your code(delegate with combobox) and my solution works if I select something in combobox and confirm that by enter clicking for example. But if you want to get solution where data will be changed automatically, when you select another item in combobox(without pressing enter) then see next case:
Create special signal onside delegate:
signals:
void boxDataChanged(const QString & str);
Create connection inside createEditor() method:
QWidget *ItemDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QComboBox *editor = new QComboBox(parent);
connect(editor,SIGNAL(currentIndexChanged(QString)),this,SIGNAL(boxDataChanged(QString)));
return editor;
}
And use it!
ItemDelegate *del = new ItemDelegate;
ui->tableView->setItemDelegate( del);
ui->tableView->setModel(model);
connect(del,&ItemDelegate::boxDataChanged,[=](const QString & str) {
//you found, just get data and use it as you want
qDebug() << str;
});