Drag and drop QStandardItem holding file paths to another application in C++? - c++

I have a treeview which is modeling a file tree. Each item in the TreeView is a QstandardItem that holds a file path. I would like to be able to get the file it referres to and drag the item into another application. The files are all video files so I would like to add the ability to drag and drop into VLC, Adobe Premier etc.

minialistic code :
main.cpp
drag d:/1.txt drop to notepad
#include <QtWidgets/QApplication>
#include <QMimeData>
#include <QTreeView>
#include <QDrag>
#include <QStandardItemModel>
#include<QUrl>
class Myodel :public QStandardItemModel
{
Q_OBJECT
public:
QStringList mimeTypes() const override
{
return QStringList(QLatin1String("text/uri-list"));
}
QMimeData* mimeData(const QModelIndexList& indexes) const override
{
QList<QUrl> urls;
QList<QModelIndex>::const_iterator it = indexes.begin();
for (; it != indexes.end(); ++it)
if ((*it).column() == 0)
urls << QUrl::fromLocalFile("d:/1.txt");
QMimeData* data = new QMimeData();
data->setUrls(urls);
return data;
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Myodel* model = new Myodel;
QStandardItem* parentItem = model->invisibleRootItem();
for (int i = 0; i < 4; ++i) {
QStandardItem* item = new QStandardItem(QString("item %0").arg(i));
parentItem->appendRow(item);
parentItem = item;
}
QTreeView* tree = new QTreeView();
tree->setModel(model);
tree->setDragEnabled(true);
tree->show();
return a.exec();
}
#include"main.moc"

Related

Using QTreeView, how to highlight only specific row/colum with calling function?

I am using C++ Qt5. Currently I have a QStandardItemModel being displayed as a QTreeView with multiple rows and columns. I am aware of using setStyleSheet(), but the issue there is that every row and column that the mouse hovers is highlighted.
I would only like specific rows of the first column to be highlighted, and then have a function called for each cell highlighted that I would then use to manipulate my game.
The solution for a personalized painting is to use a custom delegate, and to indicate which item should change the color a role should be used, in the following code I show an example:
#include <QApplication>
#include <QStandardItemModel>
#include <QStyledItemDelegate>
#include <QTreeView>
class StyledItemDelegate: public QStyledItemDelegate{
public:
using QStyledItemDelegate::QStyledItemDelegate;
protected:
void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const{
QStyledItemDelegate::initStyleOption(option, index);
if(index.data(Qt::UserRole +1).toBool())
option->backgroundBrush = QBrush(Qt::red);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTreeView w;
StyledItemDelegate delegate(&w);
w.setItemDelegate(&delegate);
QStandardItemModel model;
model.setColumnCount(4);
w.setModel(&model);
for(int i=0; i<4; i++){
auto it = new QStandardItem(QString::number(i));
model.appendRow(it);
for(int j=0; j<3; j++){
it->appendRow(new QStandardItem(QString("%1-%2").arg(i).arg(j)));
}
}
QObject::connect(&w, &QTreeView::clicked, [&](const QModelIndex & index){
bool last_state = model.data(index, Qt::UserRole +1).toBool();
model.setData(index, !last_state, Qt::UserRole +1);
});
w.expandAll();
w.show();
return a.exec();
}

QWidget in QTreeWidgetItem disappearing after reordering the QTreeWidgetItem

I have subclassed QTreeWidget (called it ToolsSelectorWidget) and enabled reordering in it by overriding QTreeWidget::dropEvent()
void ToolsSelectorWidget::dropEvent(QDropEvent *event) {
QModelIndex droppedIndex = indexAt(event->pos());
if( !droppedIndex.isValid() || droppedIndex.parent().isValid()) {
return;
}
QTreeWidget::dropEvent(event);
}
Also, I am adding QWidgets (QPushButton, QLineEdit) to top level items of QTreeWidget:
ToolsSelectorWidget::ToolsSelectorWidget(QWidget *parent) : QTreeWidget(parent) {
header()->hide();
setSelectionMode(QAbstractItemView::SingleSelection);
setDragEnabled(true);
viewport()->setAcceptDrops(true);
setDropIndicatorShown(true);
setDragDropMode(QAbstractItemView::InternalMove);
for(int i=0; i<4; ++i) {
QTreeWidgetItem *part = new QTreeWidgetItem(this);
part->setFlags(part->flags() & Qt::ItemFlag((~Qt::ItemIsDropEnabled)));
setItemWidget(part, 0, new QLabel("Part" + QString::number(i) + " Preview", this));
setItemWidget(part, 1, new QLineEdit("Part" + QString::number(i) + " Name", this));
setItemWidget(part, 2, new QCheckBox("Part" + QString::number(i) + " Visible", this));
setItemWidget(part, 3, new QCheckBox("Part" + QString::number(i) + " Locked", this));
}
}
So now I have 4 top level items each containing 4 QWidgets. It's populating them fine, but when I rearrange them by drag and drop the QWidgets disappear and I end up having an empty row. What should I do to preserve them?
Before:
After Part2 has been moved and is under Part4, it's children have been preserved, but it's conents, which are QWidgets, are gone:
Why are widgets deleted?
When the drag and drop is performed, the data of the selected items is coded (roles and associated values) and saved in a QMimeData. When the drop is accepted, the source items are deleted and new items are created with the information stored in the QMimeData, inside the saved information there is no widgets information since this does not have relation with the model. And since the items are deleted, their widgets are also deleted.
To check it we can use the following example
#include <QApplication>
#include <QLabel>
#include <QTreeWidget>
#include <QDebug>
static void on_destroyed(){
qDebug()<<"destroyed";
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTreeWidget w;
w.setSelectionMode(QAbstractItemView::SingleSelection);
w.setDragEnabled(true);
w.viewport()->setAcceptDrops(true);
w.setDropIndicatorShown(true);
w.setDragDropMode(QAbstractItemView::InternalMove);
for(int i=0; i< 5; i++){
QTreeWidgetItem *it = new QTreeWidgetItem(&w);
QLabel *lbl = new QLabel(QString::number(i));
QObject::connect(lbl, &QObject::destroyed, on_destroyed);
w.setItemWidget(it, 0, lbl);
}
w.show();
return a.exec();
}
It shows that the widgets will emit the signal they destroy when you drag and drop the items.
Possible workaround:
One possible solution is to remove the widgets before accepting the drop and set them in the new items which I have not implemented.
I have explored another solution, it is to change the QTreeWidget for a QTreeView + QStandardItemModel. In the case of the QCheckBox, the checkboxes with the Qt::ItemIsUserCheckable flag are enabled, in the case of the QLineEdit a delegate will be used and to always be shown, the openPersistentEditor() method must be used.
#include <QApplication>
#include <QStandardItemModel>
#include <QTreeView>
#include <QHeaderView>
#include <QDropEvent>
#include <QStyledItemDelegate>
#include <QLineEdit>
class ToolsSelectorDelegate: public QStyledItemDelegate{
public:
using QStyledItemDelegate::QStyledItemDelegate;
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const{
QLineEdit *le = new QLineEdit(parent);
return le;
}
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const{
QRect r(option.rect);
r.adjust(2, 2, -2, -2);
editor->setGeometry(r);
}
};
class ToolsSelectorWidget: public QTreeView{
QStandardItemModel model;
public:
ToolsSelectorWidget(QWidget *parent=nullptr): QTreeView(parent){
setItemDelegate(new ToolsSelectorDelegate(this));
setModel(&model);
header()->hide();
setSelectionMode(QAbstractItemView::SingleSelection);
setDragEnabled(true);
viewport()->setAcceptDrops(true);
setDropIndicatorShown(true);
setDragDropMode(QAbstractItemView::InternalMove);
for(int i=0; i<4; ++i) {
QList<QStandardItem *> items;
for(const QString & text: {"Preview", "Name", "Visible", "Locked"}){
QStandardItem *it = new QStandardItem(QString("Part%1 %2").arg(i).arg(text));
it->setFlags(it->flags() & ~Qt::ItemIsDropEnabled & ~Qt::ItemIsEditable);
items.append(it);
if(text == "Visible" || text == "Locked"){
it->setFlags(it->flags() | Qt::ItemIsUserCheckable);
it->setCheckState(Qt::Unchecked);
}
else if (text == "Name") {
it->setFlags(it->flags() | Qt::ItemIsEditable);
}
}
for(const QString & children: {"The", "quick", "Brown", "fox", "jump...", "over", "the", "lazy", "dog"})
items.first()->appendRow(new QStandardItem(children));
model.invisibleRootItem()->appendRow(items);
for( int i = 0; i < model.rowCount(); ++i )
openPersistentEditor(model.index(i, 1));
}
}
protected:
void dropEvent(QDropEvent *event) {
QModelIndex droppedIndex = indexAt(event->pos());
if( !droppedIndex.isValid() || droppedIndex.parent().isValid())
return;
QTreeView::dropEvent(event);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ToolsSelectorWidget w;
w.show();
return a.exec();
}

Qt assign lines from textfile to qlabels

What works so far: opening a textfile via QFileDialog and showing the contents of the textfile in a Qlabel(showfile).
I too have a for-loop that counts how many \n are in the textfile.
Now what I want is that line after line in the textfile is assigned to a new Qlabel meaning each Qlabel contains one line of the textfile and place it dynamically on runtime.
Maybe you can help since I´m a little stuck here.
Here´s my qwidget class where the labels should be placed:
class mywidget:public QWidget //class for displaying everything
{
Q_OBJECT
private:
QGridLayout *area;
QLabel *showfile; //shows input from textfile
QLabel *processname,*status; //captionlabel
QFont *pfont,*sfont; //fontoption for processname&status
QLabel **processes; //2D array to dynamically create QLabels for each entry in file
public:
mywidget(QWidget *parent = Q_NULLPTR, Qt::WindowFlags flags = 0):QWidget(parent,flags)
{
this->area = new QGridLayout(this);
this->showfile = new QLabel(tr("Test"),this);
this->pfont = new QFont();
this->pfont->setPixelSize(20);
this->sfont = new QFont();
this->sfont->setPixelSize(20);
this->processname = new QLabel(tr("Process_Name:"),this);
this->processname->setFont(*pfont);
this->status = new QLabel(tr("Status:"),this);
this->status->setFont(*sfont);
this->area->addWidget(this->processname,0,0,Qt::AlignHCenter);
this->area->addWidget(this->status,0,1,Qt::AlignHCenter);
this->area->addWidget(this->showfile,1,0,Qt::AlignHCenter);
this->area->setSpacing(10);
}
~mywidget()
{
delete this->area;
delete this->showfile;
delete this->pfont;
delete this->sfont;
delete this->processname;
delete this->status;
}
friend class mywindow;
};
And here is my open-method from QMainwindow class:
void mywindow::oeffnen()
{
this->openfilename = QFileDialog::getOpenFileName //open textfile dialog
(this,
tr("Datei öffnen"),
QDir::homePath(),
"Textdateien (*.txt *.docx *.doc);;" "Alle Dateien (*.*)"
);
if(!this->openfilename.isEmpty())
{
this->file = new QFile(this->openfilename);
this->file->open(QIODevice::ReadOnly);
this->stream = new QTextStream(this->file);
this->fileread = this->stream->readAll();
for(int z = 0;z<this->fileread.length();++z) //check entries in string by counting \n
{
this->eintraege = this->fileread.count(QRegExp("\n"));
}
//this->s_eintraege = QString::number(this->eintraege); //converting to string for displaying
this->central->showfile->setText(this->fileread); //assign filecontent to label
if(!this->file->isReadable())
{
QMessageBox::information(this,
tr("Fehler"),
tr("Konnte Datei %1 nicht laden!").arg(this->openfilename)
);
}
else
{
QMessageBox::information(this,
tr("OK"),
tr("Konnte Datei %1 laden!").arg(this->openfilename)
);
}
this->file->close();
}
}
You can add a new QLabel into a layout for each new line you read from a file. You can store the labels in a container like QVector so you can access their text later on. Here is an example:
#include <QApplication>
#include <QWidget>
#include <QLabel>
#include <QFile>
#include <QLayout>
#include <QTextStream>
#include <QDebug>
class DisplayWidget : public QWidget
{
Q_OBJECT
public:
DisplayWidget(QWidget *parent = 0) : QWidget(parent)
{
labelLayout = new QVBoxLayout;
setLayout(labelLayout);
resize(200, 200);
}
void addLabel(const QString &text)
{
QLabel *label = new QLabel(text);
label_vector.append(label);
labelLayout->addWidget(label);
}
void readFile(const QString &filename)
{
QFile file(filename);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream ts(&file);
while(!ts.atEnd())
{
QString line = ts.readLine();
if(!line.isEmpty())
addLabel(line);
}
}
QString getLabelText(int index)
{
if(label_vector.size() > index)
return label_vector[index]->text();
return QString();
}
private:
QVBoxLayout *labelLayout;
QVector<QLabel*> label_vector;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
DisplayWidget w;
w.readFile("somefile.txt");
w.show();
qDebug() << w.getLabelText(3);
return a.exec();
}
#include "main.moc"

QT QStandardItemModel segfaulting

I'm pretty new to QT, and I'm trying to take a list from a text file and output it into QT with nice formatting.
I managed to get the list printed on the window, but it has to be able to be sorted.
I have the radio buttons set up right now so that one of them displays the list and the other clears the list.
The problem is that when I switch from the list to the cleared list back to the list the program segfaults and I don't understand why.
The files are here.
winelist.cpp
#include "winelist.h"
#include "ui_winelist.h"
#include <QFile>
#include <QString>
#include <QStandardItemModel>
wineList::wineList(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::wineList)
{
ui->setupUi(this);
ui->ratingButton->setChecked(true);
fillList();
model->setHorizontalHeaderItem(0, new QStandardItem(QString("Wine Name")));
model->setHorizontalHeaderItem(1, new QStandardItem(QString("Vintage")));
model->setHorizontalHeaderItem(2, new QStandardItem(QString("Rating")));
model->setHorizontalHeaderItem(3, new QStandardItem(QString("Price")));
ui->listOutput->setModel(model);
}
wineList::~wineList()
{
delete ui;
}
void wineList::on_sortButton_clicked()
{
if( ui->ratingButton->isChecked())
{
for (int i = 0; i < 100; i++) {
model->setItem(i,0,wList[i].wineName);
model->setItem(i,1,wList[i].vintage);
model->setItem(i,2,wList[i].rating);
model->setItem(i,3,wList[i].price);
}
}
else
{
for(int i = 0; i < 100; i++) {
for(int j = 0; j < 4; j++) {
model->setItem(i, j, new QStandardItem(QString("")));
}
}
}
ui->listOutput->resizeColumnsToContents();
ui->listOutput->resizeRowsToContents();
}
void wineList::fillList()
{
Wine wine;
QString line;
QStringList lineElements;
QFile wineText(":/winelist.txt");
if (wineText.open(QIODevice::ReadOnly))
{
while ((line = line.fromUtf8(wineText.readLine())) != "")
{
lineElements = line.split(";");
lineElements[0].replace("\t", "");
lineElements[1].replace("\t", "");
wine.wineName = new QStandardItem(QString(lineElements.at(0)));
wine.vintage = new QStandardItem(QString(lineElements.at(1)));
wine.rating = new QStandardItem(QString::number(lineElements.at(2).toInt()));
wine.price = new QStandardItem(QString::number(lineElements.at(3).toInt()));
wList.append(wine);
}
}
wineText.close();
}
winelist.h
#ifndef WINELIST_H
#define WINELIST_H
#include <QMainWindow>
#include <QStandardItem>
#include <QStandardItemModel>
namespace Ui {
class wineList;
}
struct Wine {
QStandardItem* wineName;
QStandardItem* vintage;
QStandardItem* rating;
QStandardItem* price;
};
class wineList : public QMainWindow
{
Q_OBJECT
public:
explicit wineList(QWidget *parent = 0);
~wineList();
private slots:
void on_sortButton_clicked();
private:
Ui::wineList *ui;
QVarLengthArray<Wine> wList;
QStandardItemModel *model = new QStandardItemModel(100, 4, this);
void fillList();
void printList(QStandardItemModel *model);
};
#endif // WINELIST_H
main.cpp
#include "winelist.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
wineList w;
w.show();
return a.exec();
}
Clicking sort the first time
Switching Radio Button and clicking sort again
Switching Radio Button back and clicking sort again
Any Help is appreciated, I am completely lost here.
In the on_sortButton_clicked you're trying to read data from a list, but not doing any range checks. Instead, you've hardcoded 100 there.
You should rewrite this:
for (int i = 0; i < 100; i++) {
model->setItem(i,0,wList[i].wineName);
to this:
for (int i = 0; i < wList.size(); i++) {
model->setItem(i,0,wList[i].wineName);
--upd---
When you initially populate your model, it takes ownership over items from wList. When you replace model items with empty ones, it deletes initial items from wList. After this your wList is no move valid, because it contains Wine structs with dangling pointers. That's why when you try to populate your model second time, it crashes.

How to "Print Out" the data of a QStandardItem

Is there a way to print the data of a QstandardItem out, say I have;
QList<QStandardItem*> testQList;
QString yay = "!Yay";
QStandardItem *item = new QStandardItem(yay);
testQList.append(item);
qDebug() << testQList;
I just get the memory addres, (0x409bd00) I cannot dereference the list either.
You get this because you try to print whole list with objects, it is not list with strings. In this case qDebug always prints memory address so you should use loop and text() method(iterate throw list).
for(int i = 0; i<testQList.size();i++)
{
qDebug() << testQList.at(i)->text();
}
#include <QCoreApplication>
#include <QString>
#include <QList>
#include <QDebug>
class QStandardItem
{
QString mStr;
public:
QStandardItem(QString str)
{
mStr = str;
}
QString toString()
{
return mStr;
}
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QList<QStandardItem*> testQList;
QString yay = "!Yay";
QStandardItem *item = new QStandardItem(yay);
testQList.append(item);
for(int i = 0; i<testQList.size();i++)
{
qDebug() << testQList.at(i)->toString();
}
return a.exec();
}