Answer the question
In order to leave comments, you need to log in
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;
}
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_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();
}
Answer the question
In order to leave comments, you need to log in
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/
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.
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 questionAsk a Question
731 491 924 answers to any question