Results 1 to 5 of 5

Thread: problem in adding Qstring in 1st column in Qtableview/Qtablewidget

  1. #1
    Join Date
    Mar 2019
    Posts
    7
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Exclamation 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);

    }

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    451
    Thanks
    9
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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.

  3. #3
    Join Date
    Mar 2019
    Posts
    7
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: problem in adding Qstring in 1st column in Qtableview/Qtablewidget

    Quote Originally Posted by Ginsengelf View Post
    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

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,689
    Thanks
    257
    Thanked 750 Times in 740 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default 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:

    Qt Code:
    1. for each file: (all this code is inside the foreach loop)
    2. {
    3. - create the (empty) list
    4. - create a QStandardItem
    5. - set the text for that item to the file name
    6. - push that item onto the list
    7. - create a second QStandardItem
    8. - retrieve the status for the file
    9. - set that text on the second item
    10. - push the second item onto the list
    11. - append a row to the table using that two item list
    12. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by d_stranz; 12th February 2020 at 20:00.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Mar 2019
    Posts
    7
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: problem in adding Qstring in 1st column in Qtableview/Qtablewidget

    Quote Originally Posted by d_stranz View Post
    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:

    Qt Code:
    1. for each file: (all this code is inside the foreach loop)
    2. {
    3. - create the (empty) list
    4. - create a QStandardItem
    5. - set the text for that item to the file name
    6. - push that item onto the list
    7. - create a second QStandardItem
    8. - retrieve the status for the file
    9. - set that text on the second item
    10. - push the second item onto the list
    11. - append a row to the table using that two item list
    12. }
    To copy to clipboard, switch view to plain text mode 

    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...

Similar Threads

  1. Replies: 5
    Last Post: 14th August 2015, 11:23
  2. Problem when adding a column to a QAbstractProxyModel.
    By ma.renaud in forum Qt Programming
    Replies: 1
    Last Post: 5th March 2014, 00:20
  3. Replies: 1
    Last Post: 15th February 2012, 15:56
  4. Replies: 1
    Last Post: 16th November 2011, 00:10
  5. QString - problem with adding strings
    By pitterb in forum Newbie
    Replies: 1
    Last Post: 13th January 2009, 21:03

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.