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. Adding custom widgets to QTableWidget
Forum Updated to NodeBB v4.3 + New Features

Adding custom widgets to QTableWidget

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 10.9k 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.
  • B Offline
    B Offline
    Branskins
    wrote on last edited by
    #1

    I am having a problem adding a custom widget to a QTableWidget.

    In my custom QWidget constructor, I have:
    @TaskListItem::TaskListItem(QWidget *parent)
    : QWidget(parent)
    {
    this->widgetLayout = new QVBoxLayout();

    this->taskName = new QLineEdit("Test!");
    
    this->widgetLayout->addWidget(this->taskName);
    
    this->setLayout(this->widgetLayout);
    

    }
    @

    And then I add my item:
    @
    this->taskListTableView = new QTableWidget(0, 1);
    this->taskListTableView->setHorizontalHeaderLabels(labels);

    int row = this->taskListTableView->rowCount();
    this->taskListTableView->insertRow(row);
    this->taskListTableView->setCellWidget(row, 1, new TaskListItem());
    this->taskListTableView->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
    
    tableViewLayout->addWidget(this->taskListTableView);
    

    @

    I get one row that is editable upon clicking it, but it does not display my default text "Test!"

    I do not know what is going on. Can anyone guide me? Thanks!

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

      "hey just check this link this might help you"
      http://www.qtcentre.org/threads/33033-Populating-a-QtableWidget-using-a-list-of-custom-widgets

      There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code.

      1 Reply Last reply
      0
      • B Offline
        B Offline
        Branskins
        wrote on last edited by
        #3

        Thanks.

        I am trying this:
        TaskListItem *item = new TaskListItem();
        this->taskListTableView->setCellWidget(row, 1, item);
        QWidget *widget = this->taskListTableView->cellWidget(row, 1);

        if (widget == 0)
            qDebug() << "Hm";
        

        And it keeps printing Hm, meaning that the widget is not getting back an item. Why would this happen?

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

          @QWidget *widget = this->taskListTableView->cellWidget(row, 1);@

          this is not correct, first u initialize your qwidget object like this....

          @QWidget *widget =new QWidget(this);@

          just try this...

          There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code.

          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