problem in adding Qstring in 1st column in Qtableview/Qtablewidget
I want to diplay content of files in 1st column and sttaus in 2nd column..i got file name in QstringList but dont know how to diplkay in 1st column.
QtDialog::QtDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::QtDialog)
{
ui->setupUi(this);
assert(!ui->table->model());
ui->table->setModel(new QStandardItemModel(this));
assert( ui->table->model());
assert(dynamic_cast<QStandardItemModel*>(ui->table->model()));
dynamic_cast<QStandardItemModel*>(ui->table->model())->setColumnCount(2);
dynamic_cast<QStandardItemModel*>(ui->table->model())->setHeaderData(0,Qt::Horizontal,"FileName");
dynamic_cast<QStandardItemModel*>(ui->table->model())->setHeaderData(0,Qt::Horizontal,"Status");
ui->table->setColumnWidth(0, 200);
ui->table->setColumnWidth(1, 40);
view_table();
}
QtDialog::~QtDialog()
{
delete ui;
}
void QtDialog::view_table()
{
QList<QStandardItem*> items;
QList<QStandardItem*> item_file;
QList<QString> file_name;
QStringList list_item;
QString str_filename;
QDir dir("C:\\test_dir\\);
//2.
{
QStandardItem * const item = new QStandardItem;
//item->setEditable(false);
/*************/
//Check Whether Name is blank or no
if(!dir.exists())
{
//qDebug() << " not exists ";
QMessageBox::warning(this,"Warning","Cant find the any files of Server path");
}
else
{
qDebug() << " It exists ";
dir.setFilter(QDir::Files | QDir::NoSymLinks );
QFileInfoList list=dir.entryInfoList();
// QFilist=dir.entryInfoList();
for(int i=0;i<list.size();i++)
{
QFileInfo fileinfo=list.at(i);
list_item << fileinfo.fileName();
qDebug() << "String file-name" << fileinfo.fileName();
}
}
//What to be done here??
items.push_back(item);
}
//3.
//4.
{
QStandardItem * const item = new QStandardItem;
item->setEditable(true);
//item->setText("Is this a useful question?");
items.push_back(item);
}
assert(dynamic_cast<QStandardItemModel*>(ui->table->model()));
assert(dynamic_cast<QStandardItemModel*>(ui->table->model())->columnCount() == items.size());
dynamic_cast<QStandardItemModel*>(ui->table->model())->appendRow(items);
ui->table->scrollToBottom();
ui->table->setCurrentIndex(items[2]->index());
ui->table->setRowHeight(items[0]->index().row(),24);
}
Re: problem in adding Qstring in 1st column in Qtableview/Qtablewidget
Hi, you have the file name, so create a QFile object, and read its content as described in the documentation for QFile, e.g. the paragraph "Reading Files Directly" or "Using Streams to Read Files".
Then create a QStandardItem, use QStandardItem::setText(), and add it to your model using QStandardItemModel::setItem().
Ginsengelf
edit: and please wrap code in CODE tags for better readability.
Re: problem in adding Qstring in 1st column in Qtableview/Qtablewidget
Quote:
Originally Posted by
Ginsengelf
Hi, you have the file name, so create a QFile object, and read its content as described in the documentation for QFile, e.g. the paragraph "Reading Files Directly" or "Using Streams to Read Files".
Then create a QStandardItem, use QStandardItem::setText(), and add it to your model using QStandardItemModel::setItem().
Ginsengelf
edit: and please wrap code in CODE tags for better readability.
i use below code:
QStandardItem * const item = new QStandardItem();
foreach (str_filename, list_item) {
item->setText(str_filename);
}
items.push_back(item);
dynamic_cast<QStandardItemModel*>(ui->table->model())->appendRow(items);
if QListitem is { t1.txt,t2.txt,t3.txt,t4.txt} ,then it will display only t4.txt (may be because of Qstandarditem::setText).plz help
Re: problem in adding Qstring in 1st column in Qtableview/Qtablewidget
In the constructor, you set the column count for your model to 2.
In the code you just posted, you are creating a list that (is supposed to) contain one item for every file in your directory. You then (think you) are appending -one- row for each of those file names. There could 100 of them, almost certainly more than 2.
But that's not what your code is doing. In the code you posted above, you create -one- QStandardItem and use it over and over again for each filename. After you exit the loop, your -one- QStandardItem contains the name of the -last- filename. You then push that onto the list, and that list now contains only -one- item. So you add one row to the table, and it contains only the filename in the first column.
Your code should be doing this:
Code:
for each file: (all this code is inside the foreach loop)
{
- create the (empty) list
- set the text for that item to the file name
- push that item onto the list
- retrieve the status for the file
- set that text on the second item
- push the second item onto the list
- append a row to the table using that two item list
}
And PLEASE use CODE tags when posting code. Your posts are almost completely unreadable without them. See my signature below to learn how to do that.
Re: problem in adding Qstring in 1st column in Qtableview/Qtablewidget
Quote:
Originally Posted by
d_stranz
In the constructor, you set the column count for your model to 2.
In the code you just posted, you are creating a list that (is supposed to) contain one item for every file in your directory. You then (think you) are appending -one- row for each of those file names. There could 100 of them, almost certainly more than 2.
But that's not what your code is doing. In the code you posted above, you create -one- QStandardItem and use it over and over again for each filename. After you exit the loop, your -one- QStandardItem contains the name of the -last- filename. You then push that onto the list, and that list now contains only -one- item. So you add one row to the table, and it contains only the filename in the first column.
Your code should be doing this:
Code:
for each file: (all this code is inside the foreach loop)
{
- create the (empty) list
- set the text for that item to the file name
- push that item onto the list
- retrieve the status for the file
- set that text on the second item
- push the second item onto the list
- append a row to the table using that two item list
}
And PLEASE use CODE tags when posting code. Your posts are almost completely unreadable without them. See my signature below to learn how to do that.
Thanks it works...