I really need some help updating my QTreeView. I have been looking all over the forum for solutions but I couldn't find any that worked...
This is the code I got:
PlaylistModel* plm = NULL;
MainWindow::MainWindow()
{
setupUi(this);
plm = new PlaylistModel(this);
treeView->setModel(plm);
}
PlaylistModel* plm = NULL;
MainWindow::MainWindow()
{
setupUi(this);
plm = new PlaylistModel(this);
treeView->setModel(plm);
}
To copy to clipboard, switch view to plain text mode
extern Playlist* currentPlaylist;
{
if(!index.isValid())
if(index.row() >= currentPlaylist->size())
if(role == Qt::DisplayRole)
{
Song* song = currentPlaylist->at(index.row());
if(song)
{
std::cout << "Song found!" << std::endl;
if(song->info)
{
std::cout << "Info found!" << std::endl;
if(index.column() == 0)
return "Title";
else if(index.column() == 1)
return "Artist";
}
}
return "N/A";
}
else
}
QVariant PlaylistModel
::headerData(int section, Qt
::Orientation orientation,
int role
) const {
if(role != Qt::DisplayRole)
if(orientation == Qt::Horizontal)
return QString("Column %1").
arg(section
);
else
return QString("Row %1").
arg(section
);
}
int PlaylistModel
::rowCount(const QModelIndex &parent
) const {
return currentPlaylist->size();
}
int PlaylistModel
::columnCount(const QModelIndex &parent
) const {
return 2;
}
void PlaylistModel::update()
{
QModelIndex bottomRight
= createIndex
(currentPlaylist
->size
()-1,
1);
dataChanged(topLeft, bottomRight);
}
extern Playlist* currentPlaylist;
QVariant PlaylistModel::data(const QModelIndex &index, int role) const
{
if(!index.isValid())
return QVariant();
if(index.row() >= currentPlaylist->size())
return QVariant();
if(role == Qt::DisplayRole)
{
Song* song = currentPlaylist->at(index.row());
if(song)
{
std::cout << "Song found!" << std::endl;
if(song->info)
{
std::cout << "Info found!" << std::endl;
if(index.column() == 0)
return "Title";
else if(index.column() == 1)
return "Artist";
}
}
return "N/A";
}
else
return QVariant();
}
QVariant PlaylistModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if(role != Qt::DisplayRole)
return QVariant();
if(orientation == Qt::Horizontal)
return QString("Column %1").arg(section);
else
return QString("Row %1").arg(section);
}
int PlaylistModel::rowCount(const QModelIndex &parent) const
{
return currentPlaylist->size();
}
int PlaylistModel::columnCount(const QModelIndex &parent) const
{
return 2;
}
void PlaylistModel::update()
{
QModelIndex topLeft = createIndex(0, 0);
QModelIndex bottomRight = createIndex(currentPlaylist->size()-1, 1);
dataChanged(topLeft, bottomRight);
}
To copy to clipboard, switch view to plain text mode
As you can see, the data is stored in currentPlaylist (the Playlist class is just a QList). But when I add data to the currentPlaylist, it doesn't show it on the treeView... But when I add data to the currentPlaylist before I construct the tableView and model, it does show the data!
So I have tried to make an update function. But that doesn't work either...
Thanks in advance,
Gillis
Bookmarks