gpt4 book ai didi

c++ - Qt : QTreeView display two string on a row

转载 作者:太空宇宙 更新时间:2023-11-04 13:37:20 25 4
gpt4 key购买 nike

我正在使用 Qt 5.4 并正在寻找一种在一行中显示两个字符串的方法,如下图所示:

enter image description here

但我能达到的最接近的结果是这个(有两行),它看起来相当可怕:

enter image description here

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 -> 构造函数

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);
}

编辑:发布源代码

最佳答案

正如 SGaist 在 this post 上建议的那样我只需要做:

_ui->DevicesAndPlaylistList->setSelectionBehavior(QAbstractItemView::SelectRows);

现在它就像一个魅力:)

关于c++ - Qt : QTreeView display two string on a row,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29129119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com