Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Populating a TableWidget...Best Way of doing it?
QtWS25 Last Chance

Populating a TableWidget...Best Way of doing it?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    petergeng
    wrote on last edited by
    #1

    I have a data set, in the form of a matrix... column names are people names, row names are certain attributes... each cell could be a list of 3 radio buttons OR a dropdown box. So i was thinking of using a TableWidget for all of this.

    column names are read from file, row names are passed from another function.

    should i use TableWidgetItem to add the cells and information? or use the StandardItemModel to add cells and information?

    and can somebody show me some sample code in adding such information??

    thanks a bunch!

    1 Reply Last reply
    0
    • P Offline
      P Offline
      petergeng
      wrote on last edited by
      #2

      I have tried with both TabelWidgetItem and StandardItemModel, both of them dont work....
      @QFile file("../engD.txt");
      if (!file.exists())
      {
      QMessageBox error;
      error.setText("error");
      error.exec();
      }
      if (!file.open(QFile::ReadOnly|QFile::Text))
      {
      qDebug();
      }
      QTextStream stream(&file);
      QString mText;
      while (!stream.atEnd())
      {
      for (int row =0; row<4; row++)
      {
      for (int col =0; col<4;col++)
      {
      mText= stream.readLine();
      QTableWidgetItem *newItem = new QTableWidgetItem;
      newItem->setText(mText);
      ui->tableWidget->setItem(row, col, newItem);
      }
      }
      }

      QStandardItemModel *model = new QStandardItemModel(4,4);
      model->setHeaderData(0,Qt::Horizontal,mText);
      ui->tableWidget->setModel(model);
      file.close(); @

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        First, please re-edit your post an indent your code, it's quite unreadable this way.

        For your actual problem:
        You set the items in your loops and in the end you destroy all the work you've done by setting a new (and empty!) model on your table widget.

        The way to go is:

        Either use [[Doc:QTableWidget]] together with [[Doc:QTableWidgetItem]]s

        Or use [[Doc:QTableView]] together with a [[Doc:QStandardItemModel]]

        You should not mix a QTableWidget with any item model, being it standard or custom.

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved