Qt: QTreeView 连续显示两个字符串

Qt: QTreeView display two string on a row

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

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

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

现在它就像一个魅力:)