T
T
TaleFrance2021-03-08 09:28:26
Qt
TaleFrance, 2021-03-08 09:28:26

QTableView. File I/O. How to return data from QList to table? How to FILL table from QLIST?

Hello! Please help me with the program. It is necessary to add the ability to save and load a table from a file.

Overload:

QDataStream&  operator<<(QDataStream&  stream, const MyCard& card)
{

    stream  <<  card._number  << card._name << quint32(card._sum)
             << quint32(card._profit) <<  quint32(card._rate)<<quint32(card._balance);

    return stream;
}

QDataStream& operator>>(QDataStream& stream, MyCard& card)
{
   stream  >>  card._number  >> card._name >>  card._sum >> card._profit >>  card._rate
                                                                   >>card._balance;
   // QString number;
  //  stream>>number;
    return stream;
}


Filling the Source Model with Data in the Constructor

Card::Card(QObject *parent) : QAbstractTableModel(parent)
{
   int sum = 800;
    int profit = 700;
    int rate = 300;
    int balance = sum + profit - rate;

    MyCard z("8812913", "Person1", (qint32)sum, (qint32)profit, (qint32)rate, (qint32)balance);
    _card<<z;


    int sum1 = 500;
    int profit1 = 600;
    int rate1 = 300;
    int balance1 = sum1 + profit1 - rate1;

    MyCard z1("7812913", "Person2", sum1, profit1, rate1, balance1);
    _card<<z1;

}



QList<MyCard> Card::card() const
{
    return _card;
}


void MainWindow::on_Write_triggered() // Writing
{

QString file_name = QFileDialog::getSaveFileName(this, "Open a file", "C:/files/myfile.txt");
QFile file(file_name);
QList x = _card.card(); // Get data

if (!file.open(QFile::WriteOnly))
{
QMessageBox:: warning(this, "title", "file not open");
}

QDataStream out(&file);
out.setVersion(QDataStream::Qt_4_1);

out << x; // Write table to file

file.close();
}

void MainWindow::on_Read_triggered()     // Чтение
{
    QString file_name = QFileDialog::getOpenFileName(this, "Open a file", "C:/files/myfile.txt", tr("Text Files (*.txt);;C++ Files (*.cpp *.h)"));

    QFile file(file_name);

    if (!file.open(QFile::ReadOnly))
    {
        QMessageBox:: warning(this, "title", "file not open");
    }


QDataStream in(&file);
 in.setVersion(QDataStream::Qt_4_1);
QList<MyCard> card;

in >> card;   
// КАК ЗАПОЛНИТЬ ТАБЛИЦУ ИЗ card?

if(!card.isEmpty())
{
    QMessageBox:: warning(this, "title", "ne pustoy");
}

    file.close();
}


How to implement such a filling? I don't understand what to do. You need to use only QList and QTableView
6045c3a1b19a9908228897.png

This is how I wrote it to the file (Write)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Ananiev, 2021-03-08
@SaNNy32

Implement a data source, such as a QList, into your model. Implement a method that will add data to the source in your model. Displaying data in the model, respectively, is also done from this source. https://evileg.com/en/forum/topic/419/

Y
Yakov E, 2021-03-08
@Zifix Qt tag curator

Read any book on Qt (Summerfield for example), save weeks of time on basic stuff like models .
You can also take QTableWidget, it’s easier to work with it, but it will slow down on large volumes, and the code will turn out so-so.

M
Mikhail Makarov, 2017-12-24
@NightmareZz

Doesn't work, thank God. Because shitcode. Changing the array prototype in the function that you hang on onclick is something with something.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question