Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt: QTreeView display two string on a single row
QtWS25 Last Chance

Qt: QTreeView display two string on a single row

Scheduled Pinned Locked Moved General and Desktop
multiple textsingle row5.4qtreeview
5 Posts 2 Posters 2.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NicolasDiNunno
    wrote on last edited by
    #1

    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);
    }
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Do you mean the focus rectangle that doesn't take the whole row ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • N Offline
        N Offline
        NicolasDiNunno
        wrote on last edited by
        #3

        Yes, I need a visual trick or something like that

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Then you can call setSelectionBehavior with QAbstractItemView::SelectRows and you should be good to go

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • N Offline
            N Offline
            NicolasDiNunno
            wrote on last edited by
            #5

            @SGaist Thank you ! it's now working perfectly :)

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved