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

时间:2015-03-18 17:44:48

标签: c++ qt5 qtreeview qitemdelegate

我正在使用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 - &gt;构造

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

编辑:已发布源代码

1 个答案:

答案 0 :(得分:0)

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

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

现在它就像一个魅力:)