Qt: QTreeView display two string on a single row
-
Hi, I'm using Qt 5.4 and looking for a way to display two string on a single row like this picture:
http://i.stack.imgur.com/nStIv.png
But the closest result I can achieve is this (with two rows), and it looks rather horrible:
http://i.stack.imgur.com/BgLf3.png
DevicesAndPlaylistModel.h
#ifndef DEVICESANDPLAYLISTMODEL_H #define DEVICESANDPLAYLISTMODEL_H #include <QStandardItemModel> class QModelIndex; class DevicesAndPlayListModel : public QStandardItemModel { Q_OBJECT public: DevicesAndPlayListModel(); ~DevicesAndPlayListModel(); QModelIndex indexFromInternalName(QString ); private: void insertBlankItem(); QList<QStandardItem *> _items; }; #endif // DEVICESANDPLAYLISTMODEL_H
DevicesAndPlaylistModel.cpp
#include <QDebug> #include "DevicesAndPlaylistModel.h" #include "Special/MTP/MTP.h" DevicesAndPlayListModel::DevicesAndPlayListModel() { setColumnCount(2); QStandardItem *LibraryItem = new QStandardItem(); LibraryItem->setText("Library"); LibraryItem->setFlags(Qt::NoItemFlags); LibraryItem->setEnabled(false); QStandardItem *DevicesItem = new QStandardItem(); DevicesItem->setText("Devices"); DevicesItem->setFlags(Qt::NoItemFlags); DevicesItem->setEnabled(false); QStandardItem *PlayListItem = new QStandardItem(); PlayListItem->setText("Playlists"); PlayListItem->setFlags(Qt::NoItemFlags); PlayListItem->setEnabled(false); QStandardItem *PluginsItem = new QStandardItem(); PluginsItem->setText("Plugins"); PluginsItem->setFlags(Qt::NoItemFlags); PluginsItem->setEnabled(false); QStandardItem *AlbumItem = new QStandardItem(); AlbumItem->setText("Albums"); AlbumItem->setData("Library_albumView", Qt::UserRole); QStandardItem *TrackItem = new QStandardItem(); TrackItem->setText("Track"); TrackItem->setData("Library_trackView", Qt::UserRole); QStandardItem *ArtistItem = new QStandardItem(); ArtistItem->setText("Artists"); ArtistItem->setData("Library_artistsView", Qt::UserRole); QStandardItem *ComposerItem = new QStandardItem(); ComposerItem->setText("Composer"); ComposerItem->setData("Library_composerView", Qt::UserRole); QStandardItem *GenreItem = new QStandardItem(); GenreItem->setText("Genre"); GenreItem->setData("Library_genreView", Qt::UserRole); MTP *mtp = new MTP(); QMap<QString, QString> data = mtp->getSnAndNames(); for(int deviceIndex = 0; deviceIndex < data.count(); deviceIndex++) { QList<QStandardItem *> items; QStandardItem *item = new QStandardItem(); QString SerialNumber = data.keys().at(deviceIndex); QString Name = data.values().at(deviceIndex); item->setText(Name); item->setData("Devices", Qt::UserRole); item->setData(SerialNumber, Qt::UserRole+1); QStandardItem * col = new QStandardItem("100%"); col->setTextAlignment(Qt::AlignRight); items << item << col; DevicesItem->appendRow(items); } _items << LibraryItem << DevicesItem << PlayListItem << PluginsItem << AlbumItem << TrackItem << ArtistItem << ComposerItem << GenreItem; LibraryItem->appendRow(AlbumItem); LibraryItem->appendRow(TrackItem); LibraryItem->appendRow(ArtistItem); LibraryItem->appendRow(ComposerItem); LibraryItem->appendRow(GenreItem); insertRow(rowCount(), LibraryItem); insertBlankItem(); insertRow(rowCount(), DevicesItem); insertBlankItem(); insertRow(rowCount(), PlayListItem); insertBlankItem(); insertRow(rowCount(), PluginsItem); } DevicesAndPlayListModel::~DevicesAndPlayListModel() { } QModelIndex DevicesAndPlayListModel::indexFromInternalName(QString name) { for(int itemsIndex = 0; itemsIndex < _items.count(); itemsIndex++) { if(_items.at(itemsIndex)->data(Qt::UserRole).toString() == name) return _items.at(itemsIndex)->index(); } return QModelIndex(); } void DevicesAndPlayListModel::insertBlankItem() { QStandardItem *blankItem = new QStandardItem(); blankItem->setFlags(Qt::NoItemFlags); blankItem->setEnabled(false); insertRow(rowCount(), blankItem); }
MainWindow.cpp -> constructor
MainWindowController::MainWindowController(QWidget *aParent) : QMainWindow(aParent), _ui(new Ui::MainWindowView) { _ui->setupUi(this); _devicseAndPlaylistModel = new DevicesAndPlayListModel(); //DevicesAndPlaylistList is the QTreeView _ui->DevicesAndPlaylistList->setModel(_devicesAndPlaylistModel); _ui->DevicesAndPlaylistList->header()->close(); _ui->DevicesAndPlaylistList->expandAll(); _ui->DevicesAndPlaylistList->setExpandsOnDoubleClick(false); _ui->DevicesAndPlaylistList->setRootIsDecorated(false); _ui->DevicesAndPlaylistList->setFocusPolicy(Qt::NoFocus); }
-
Hi and welcome to devnet,
Do you mean the focus rectangle that doesn't take the whole row ?
-
Yes, I need a visual trick or something like that
-
Then you can call setSelectionBehavior with QAbstractItemView::SelectRows and you should be good to go
-
@SGaist Thank you ! it's now working perfectly :)