,
IDE QT, QT , .NET
QListView, QGroupBox, QPushButton ( 䳿 )
mylistmodel.h
#ifndef MYLISTMODEL_H
#define MYLISTMODEL_H
#include <QAbstractListModel>
#include "tlinkedlist.h"
#include "string"
using namespace std;
class MyListModel: public QAbstractListModel
{
Q_OBJECT
public:
explicit MyListModel(QObject *parent = 0);
int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role) const;
void addToHead(float value, string str);
TLinkedList* getList();
void refreshAll();
signals:
public slots:
private:
TLinkedList* lst;
};
#endif
mylistmodel.cpp
#include "mylistmodel.h"
MyListModel::MyListModel(QObject *parent):
QAbstractListModel(parent)
{
lst = new TLinkedList();
}
int MyListModel:: rowCount (const QModelIndex &parent) const
{
return lst->getCount();
}
QVariant MyListModel:: data (const QModelIndex &index, int role) const
{
if(role == Qt::DisplayRole)
{
Data* temp = lst->getAt(index.row());
QString t = QString::number(temp->price);
t.append(tr(" @ "));
t.append(QString::fromStdString(temp->prod));
return t;
}
return QVariant();
}
void MyListModel::addToHead(float value, string str)
{
lst->addHead(value,str);
dataChanged(createIndex(0,0),createIndex(0,0));
}
TLinkedList *MyListModel::getList()
{
return lst;
}
void MyListModel::refreshAll()
{
dataChanged(createIndex(0,0),createIndex(lst->getCount()-1, 0));
}
mainwindow.cpp
#include "mainwindow.h"#include "ui_mainwindow.h"#include <QInputDialog>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); listModel = new MyListModel(); ui->listView-> setModel (listModel);}MainWindow::~ MainWindow (){ delete ui;}void MainWindow::on_pb_addHead_clicked(){ listModel->addToHead(QInputDialog::getDouble(this, " ", " "), (QInputDialog::getText(this, " ", " ")).toStdString());}void MainWindow::on_pb_addTail_clicked(){ listModel->getList()->addTail(QInputDialog::getDouble(this, " ", " "), (QInputDialog::getText(this, " ", " ")).toStdString()); listModel->refreshAll();}void MainWindow::on_pb_insert_clicked(){ listModel->getList()->insert(QInputDialog::getInt(this, " ", " "), (QInputDialog::getDouble(this, " ", " "), (QInputDialog::getText(this, " ", " ")).toStdString())); listModel->refreshAll();}void MainWindow::on_pb_delHead_clicked(){ listModel->getList()->delHead(); listModel->refreshAll();}void MainWindow::on_pb_delTail_clicked(){ listModel->getList()->delTail(); listModel->refreshAll();}void MainWindow::on_pb_delByIndex_clicked(){ listModel->getList()->delByIndex(QInputDialog::getInt(this, " ", " ")); listModel->refreshAll();}void MainWindow::on_delRepeated_clicked(){ listModel->getList()->delRepeated(); listModel->refreshAll();}void MainWindow::on_pb_readFromText_clicked(){ listModel->getList()->textFromFile(QApplication::applicationDirPath().append("\\tree.txt").toStdString()); listModel->refreshAll();}void MainWindow::on_pb_writeToText_clicked(){ listModel->getList()->textToFile(QApplication::applicationDirPath().append("\\tree.txt").toStdString()); listModel->refreshAll();}void MainWindow::on_pb_readFromBinary_clicked(){ listModel->getList()->binaryFromFile(QApplication::applicationDirPath().append("\\tree.bin").toStdString()); listModel->refreshAll();}void MainWindow::on_pb_writeToBinary_clicked(){ listModel->getList()->binaryToFile(QApplication::applicationDirPath().append("\\tree.bin").toStdString()); listModel->refreshAll();}
|
|
, . , , , .