As is possible to see from the print screen below I have the following problem: as I add the entry via QPushButton
I don't want the last entry to be the last of the QListView
but instead I want it to be at the top of the QListView
. How do I make the QListView always showing from the latest entry to the last entry?
The desired behavior is with the latest entry on top:
Below the code:
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
mView = new QGraphicsView();
mScene = new QGraphicsScene();
ui->graphicsView->setScene(mScene);
QFont font;
font.setPixelSize(10);
font.setBold(false);
font.setFamily("Calibri");
mText = new QGraphicsTextItem;
mText->setPos(150,70);
mScene->addText(tr("Boat outside alarm area"))->setDefaultTextColor(Qt::black);
model = new QStringListModel();
ui->listView->setModel(model);
ui->listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
connect(ui->listView, SIGNAL(loggingUpdated()), this, SLOT(updateListView(const QString &)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::updateListView(const QString & message)
{
if(model->insertRow(model->rowCount())) {
QModelIndex index = model->index(model->rowCount() - 1, 0);
model->setData(index, message);
ui->listView->scrollTo(index);
}
}
void MainWindow::on_pushButton_clicked()
{
QString str = ui->lineEdit->text();
model->insertRow(model->rowCount());
QModelIndex index = model->index(model->rowCount()-1);
model->setData(index, str);
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsTextItem>
#include <QStringListModel>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void updateListView();
void updateListView(const QString & message);
signals:
void loggingUpdated();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
QGraphicsView *mView;
QGraphicsScene *mScene;
QGraphicsTextItem *mText;
QStringListModel *model;
};
#endif // MAINWINDOW_H
I wonder if there is some missing signals I didn't catch or if at this point I should use a Q_PROPERTY
but I am not sure if that is actually the right route. I am running out of ideas on how to solve this issue.
What I have done so far:
I researched the problem and came across this source that helped me set the proper index. However it did not help to completely solve the problem.
I also consulted the official documentation however my problem is not related to how scolling should be, but how to determine the latest entry and put it as first item of the QListView
.
I didn't stop there and dug more and arrived to this command but I am not sure it applies coorectly to the problem I am solving.
Thanks for pointing to the right direction on how to solve this problem
Aucun commentaire:
Enregistrer un commentaire