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. [Solved] Help using QStandardItemModel
Forum Updated to NodeBB v4.3 + New Features

[Solved] Help using QStandardItemModel

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 3.2k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    dvillalobos
    wrote on last edited by
    #1

    Hi, I have the following code, taken from SpinBoxDelegate Example, but in my example, the QTableView does not shown the data. Any clue?

    Best regards.

    David

    @
    mdiMain = new QMdiArea;
    mdiMain->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    mdiMain->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    setCentralWidget(mdiMain);

    QMdiSubWindow *sub = new QMdiSubWindow;
    QTableView *table = new QTableView;
    QStandardItemModel model(4, 2);
    for (int row = 0; row < 4; ++row)
    for (int column = 0; column < 2; ++column)
    model.setData(model.index(row, column, QModelIndex()), QVariant((row+1) * (column+1)));
    table->setModel(&model);
    sub->setWidget(table);
    sub->show();
    @

    Regards

    David

    1 Reply Last reply
    0
    • S Offline
      S Offline
      stima_ua
      wrote on last edited by
      #2

      setRole for your data. For example Qt::DisplayRole.

      @
      model.setData(model.index(row, column, QModelIndex()), QVariant((row+1) * (column+1)), Qt::DisplayRole);
      @

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dvillalobos
        wrote on last edited by
        #3

        Hi stima_ua, thanks for your replay, but it did not work.

        Regards

        David

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Scylla
          wrote on last edited by
          #4

          How long is your model alive!?!?

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dvillalobos
            wrote on last edited by
            #5

            Hi Scylla, thanks for your replay.

            What do you mean how long is my model alive?

            I have noticed that in my example, when I refresh the model contents by filling it, the QTableView seems to be filled but just for about a second.

            Regards

            David

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Scylla
              wrote on last edited by
              #6

              I don't know where your code is located. If it's not in the main() your model will die on the end of the scope. That's what I mean.

              So if i's not your issue, please show more code.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                dvillalobos
                wrote on last edited by
                #7

                Great!!! Scylla.

                You hit it, :-) in my example model has to be declared in the same place mdiMain was decladed.

                I really appreciate your help and stima_ua, thanks for your help too.

                @
                mdiMain = new QMdiArea;
                mdiMain->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
                mdiMain->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
                setCentralWidget(mdiMain);

                QMdiSubWindow *sub = new QMdiSubWindow;
                QTableView *table = new QTableView;
                model = new QStandardItemModel(4, 2);
                for (int row = 0; row < 4; ++row)
                for (int column = 0; column < 2; ++column)
                model.setData(model.index(row, column, QModelIndex()), QVariant((row+1) * (column+1)));
                table->setModel(&model);
                sub->setWidget(table);
                sub->show();
                @

                Regards

                David

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  dvillalobos
                  wrote on last edited by
                  #8

                  Now, can I add a QIcon to the vertical o horizontal headers like in QTableWidget? I mean, can I do something like this with QTableView?

                  @
                  result->setHorizontalHeaderItem(row, new QTableWidgetItem(QIcon(":/MyIcon.png"), "Header"));
                  @

                  Regards

                  David

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    Scylla
                    wrote on last edited by
                    #9

                    somthing like that:
                    @m_model->horizontalHeaderItem(0)->setIcon(QIcon(":/ressources/clean.png"));@

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      stima_ua
                      wrote on last edited by
                      #10

                      Read manual.
                      @
                      QTableWidgetItem *item = new QTableWidgetItem;
                      item->setData(QVariant(), Role);
                      item->setIcon(path);
                      @

                      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