Showing images along with text in QTableView model field - c++

I am writing a Qt GUI C++ program where I am planning to populate a tabular view, for example a 3X2 table view. Where the texts in the field will be from a .csv file. And along with the text there will also be a small icon/image.
To give an idea of the UI it might be looking somewhat like;
Now adding text to QTable model view I have done using;
QStandardItemModel *model = new QStandardItemModel;
QFile file("/home/aj/beta_test.csv");
if (file.open(QIODevice::ReadOnly))
{
int lineindex = 0; // file line counter
QTextStream in(&file); // read to text stream
while (!in.atEnd()) {
QStringList lineToken;
QString fileLine = in.readLine(); // read one line (separated by "\n")
lineToken = fileLine.split(",", QString::SkipEmptyParts); // parse a line into separate pieces with "," as the delimiter
for (int j = 0; j < lineToken.size(); j++) // load parsed data to model accordingly
{
QString value = lineToken.at(j);
QStandardItem *item = new QStandardItem(value);
model->setItem(lineindex, j, item);
ui->tableView->setModel(model);
}
lineindex++;
}
file.close();
}
Now how to perform adding the image part???

You can use standard method:
item->setIcon(QIcon("path"));
or do this with index (use setData() and Qt::DecorationRole)
After adding you can call resizeRowsToContents() to show full images in your cells.
Also I noticed that you set your model in every iteration. It is not wrong but it is very inefficient (especially when you populate large data), so set your model one time after the loop.

Related

Copy to clipboard from tableview

I have Written a code and I need to copy the selected rows in Qt C++
My OTPWindow.cpp file has
this function
SafeOTPWindow::on_tblCopy_clicked()
{
QClipboard* clip = qApp->clipboard();
clip->setText(ui->tblLog->text());
}
My OTPWindow.h file has
private slots:
void on_tblCopy_clicked();
I am getting an error
text is not an member of Qtableview. How can I solve this error
I need to copy the text contents from tableview which are in rows inside What property should I set in .cpp file. Here tblLog is my tableview.
An way to achieve what you want is get the list of the selected items and concatenate the text of these item, like as below:
QStandardItemModel *model = qobject_cast<QStandardItemModel*>(ui->tableView->model());
if (!model) //Check if listview has a model
return;
QModelIndexList indexlist = ui->tableView->selectionModel()->selectedIndexes();
QString str;
int lastrow = -1;
foreach (const QModelIndex &index, indexlist)
{
if (lastrow >= 0)
{
if (index.row() == lastrow)
str.append(Qt::Key_Space); //Add space between items in same line
else
str.append("\n"); //Add break line if entering in a new line
}
str.append(model->index(index.row(), index.column()).data().toString());
lastrow = index.row();
}
str.append("\n"); //Add break line to the end of the string
QClipboard* clipboard = QApplication::clipboard();
clipboard->setText(str); //Copy the string to clipboard

How to populate Qt ListView with content from a text file?

I have a text file that contains many vocabularies, each vocabulary is separated by a new line.
How can I populate a Qt ListView with vocabularies from the text file?
QStringListModel *model;
// Create model
model = new QStringListModel(this);
QStringList stringList;
// open the file
QFile textFile("/<FullPath>/<fileName>");
if(!textFile.open(QIODevice::ReadOnly)) {
QMessageBox::information(0,"Error",textFile.errorString());
}
// teststream to read from file
QTextStream textStream(&textFile);
while (true)
{
QString line = textStream.readLine();
if (line.isNull())
break;
else
stringList.append(line); // populate the stringlist
}
// Populate the model
model->setStringList(stringList);
// Glue model and view together
ui->listView->setModel(model);
// if you want to add additional feature to listview.
ui->listView->
setEditTriggers(QAbstractItemView::AnyKeyPressed |
QAbstractItemView::DoubleClicked);
You need to read the file line by line, and add to a QStringList, then into listView.
QStringList *allLines = new QStringList(); //Your list for lines from the file.
allLines->clear();
QStringListModel *linesModel = new QStringListModel(*allLines, NULL); //Your model to set to the view.
QFile file("/path/to/yourFileName.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
while (!file.atEnd()) {
QByteArray line = file.readLine(); //Lines are read as QByteArray.
const char *line_c = line.data(); //convert to const char*
QString line_str = QString(QLatin1String(line_c)); //And finally convert to QString
allLines->append(line_str); //Add to the list
}
linesModel->setStringList(*allLines); //Set your model's list your stringlist
listView->setModel(linesModel); //set model of your listView linesModel. You need to use your listView's name, which might be ui->listView.

Uploading .csv or .txt file to populate QTableView

Recently I was working on a gui application & I wanted to save the data of QTableView in a .csv or .txt file. I Used the guidance received during this question which made me think if the reverse is also possible; i.e. if the QTableView can be populated from a .csv or .txt file. Once again I would prefer staying with a model based design such as QTableView instead of item based QTableWidget.
Any code-snippet or tutorial-documentation would be really helpful.
Consider a test.csv file (it could be generated by any text editor):
And the textstream behind is (if generated by programming):
1,2,3,\n4,5,6,\n7,8,9,\n10,11,12,\n13,14,15,
If opened in Microsoft Office Excel, it might looked like:
To read this .csv file to the model of your QTableView:
QStandardItemModel *model = new QStandardItemModel;
QFile file("test.csv");
if (file.open(QIODevice::ReadOnly)) {
int lineindex = 0; // file line counter
QTextStream in(&file); // read to text stream
while (!in.atEnd()) {
// read one line from textstream(separated by "\n")
QString fileLine = in.readLine();
// parse the read line into separate pieces(tokens) with "," as the delimiter
QStringList lineToken = fileLine.split(",", QString::SkipEmptyParts);
// load parsed data to model accordingly
for (int j = 0; j < lineToken.size(); j++) {
QString value = lineToken.at(j);
QStandardItem *item = new QStandardItem(value);
model->setItem(lineindex, j, item);
}
lineindex++;
}
file.close();
}
(You could manipulate the code to meet you table format)
[Result]

QTableView export to .csv number of rows fetched is limited to only 256

I wrote a gui program which will connect to an oracle db an retrieve data after a query is typed. the retrieved data is shown in a QTableView table model widget. and later the result of the QTableView is exported to a .csv file
QString MyQuery = ui->lineQuery->text();
db.open();
QSqlQuery query(MyQuery,db);
if(query.exec())
{
qDebug()<<QDateTime::currentDateTime()<<"QUERY SUCCESS ";
ui->queryButton->setStyleSheet("QPushButton {background-color: rgb(0, 255, 0);}");
this->model=new QSqlQueryModel();
model->setQuery(MyQuery);
ui->tableViewOra->setModel(model);
QString textData;
int rows=model->rowCount();
int columns=model->columnCount();
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
textData += model->data(model->index(i,j)).toString();
textData += ", "; // for .csv file format
}
textData += "\n"; // (optional: for new line segmentation)
}
QFile csvfile("/home/aj/ora_exported.csv");
if(csvfile.open(QIODevice::WriteOnly|QIODevice::Truncate))
{
QTextStream out(&csvfile);
out<<textData;
}
csvfile.close();
}
now the problem is, during a query I observed there are 543 rows visible in the QTableView (which is correct cause there are 543 entries). But when the .csv file is exported, only 256 rows are there.
Is there any variable size constraint that I am unaware about ???
Which variables should be taken care of if I want to export .csv files of upto 1000 rows approx ??? thanks.
I think when you first read model->rowCount() the model has not fetched all the results completely. Although it will fetch more later when table view is displayed resulting in a full display of rows in the table view.
Try to use QSqlQueryModel::fetchMore before reading the row count :
while (model->canFetchMore())
model->fetchMore();
int rows=model->rowCount();
int columns=model->columnCount();
[Additional information from Tay2510]:
You could just change the file open flag
if(csvfile.open(QIODevice::WriteOnly|QIODevice::Truncate))
to
if(csvfile.open(QIODevice::WriteOnly))
The former will overwrite the same file while the latter append data on it.

Spliting string and displaying in Qtreewidget

My programming knowledge and experience is very poor. I am using this code block to open the desired file when clicked on a push button ;
QString filename = QFileDialog::getOpenFileName();
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
while (!file.atEnd())
{
QByteArray line = file.readLine();
processline(line);
}
And by this line i am showing it on QtextBrowser
void MainWindow::processline(QByteArray paramline)
{
ui->veri_cikis->append(paramline.constData());
}
The data on the file is like this
0;100;0
0;100;24
24;500;24
24;100;6
6;100;6
i have to split the datas by ";" mark and display them on a Qtreewidget columns. How do i do that ? And i have to show each first part on first column and second on second column and so. I have 3 columns in total
I think what you describe is better fit rather to a table view than a tree view. To parse your strings and split them by ';' character you can use QByteArray::split() function. Here is the sample code, that creates and populates table view with items that read from the file:
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTableWidget *table = new QTableWidget;
int row = 0;
while (!file.atEnd()) {
QByteArray line = file.readLine();
QList<QByteArray> tokens = line.split(';');
int column = 0;
row++;
foreach (QByteArray ba, tokens) {
QTableWidgetItem *item = new QTableWidgetItem(ba);
table->setItem(row, column++, item);
}
}