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. QTableWidget's. setText() does not set any text to cells

QTableWidget's. setText() does not set any text to cells

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 9.5k 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.
  • B Offline
    B Offline
    britsfp
    wrote on last edited by
    #1

    Hello,
    I use QTableWidgetItem to insert some text to cells in a table but all that happens is that it crashes with a segmentation error. Code below:

    QTableWidgetItem *item = new QTableWidgetItem;
    item = ui->tablewidget->item(2, 2);
    item->setText("danny");

    so when the last line executes it crashes with a segmentation fault that takes me to some at header file ?.

    any ideas?

    regards
    Frik Brits

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      You are not setting the item in the cell first. item is null.

      The fastest solution is to replace your 3 lines with:

      auto model = ui->tablewidget->model();
      model->setData(model->index(2,2),QStringLiteral("danny"));
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      5
      • I Offline
        I Offline
        israelins85
        wrote on last edited by
        #3

        Try,

        QTableWidgetItem *item = new QTableWidgetItem;
        item->setText("danny");
        ui->tablewidget->setItem(2, 2, item);

        1 Reply Last reply
        3

        • Login

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