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. Own QTableWidget Class dont add Items

Own QTableWidget Class dont add Items

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.2k 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.
  • F Offline
    F Offline
    Fuel
    wrote on last edited by
    #1

    I have an own QTableWidget Class thats only inherits from QTableWidget. The Problem is that no Items will be displayed in the Table. I have a Database where the Data comes from. And the Querie returns the correct Data. The Method i call to get the Data gets triggered, but there are no Items in my Table. I tested to add a Test QTableWidgetItem but that dont works too. Anyone has a Idea where i can search the Problem? I get no Errors.

    WeekPlanWidget::WeekPlanWidget(QWidget *parent) :
        QTableWidget(parent)
    {
        this->setColumnCount(3);
        this->resizeColumnsToContents();
        this->setSelectionBehavior(QAbstractItemView::SelectRows);
        this->horizontalHeader()->hide();
        this->setShowGrid(false);
        this->setStyleSheet("QTableView { border: 0px; }");
    
        mStatus.Undone = QIcon(":/icons/img/undone.png");
        mStatus.Pending = QIcon(":/icons/img/pending.png");
        mStatus.Done  = QIcon(":/icons/img/done.png");
    }
    
    void WeekPlanWidget::getTasks(QDate date)
    {
        mId.clear();
    
        Database *db = Database::instance();
        QVector<QList<QString>> data = db->selectTasks(date);
    
        int i = 0;
        for (QList<QString> row : data)
        {
            mId.append(row.at(0).toInt());
    
            QDateTime datetime = QDateTime::fromString(row.at(1), "yyyy-MM-dd hh:mm:ss");
            QTableWidgetItem *time = new QTableWidgetItem(datetime.time().toString("hh:mm"));
            QTableWidgetItem *task = new QTableWidgetItem(row.at(2));
            QTableWidgetItem *icon = new QTableWidgetItem();
    
            if (row.at(3).toInt() == 0)
                icon->setIcon(mStatus.Pending);
            else if (row.at(3).toInt() == 1)
                icon->setIcon(mStatus.Undone);
            else if (row.at(3).toInt() == 2)
                icon->setIcon(mStatus.Done);
    
            this->setItem(i, 0, time);
            this->setItem(i, 1, task);
            this->setItem(i, 2, icon);
            i++;
        }
    }
    
    joeQJ 1 Reply Last reply
    0
    • F Fuel

      I have an own QTableWidget Class thats only inherits from QTableWidget. The Problem is that no Items will be displayed in the Table. I have a Database where the Data comes from. And the Querie returns the correct Data. The Method i call to get the Data gets triggered, but there are no Items in my Table. I tested to add a Test QTableWidgetItem but that dont works too. Anyone has a Idea where i can search the Problem? I get no Errors.

      WeekPlanWidget::WeekPlanWidget(QWidget *parent) :
          QTableWidget(parent)
      {
          this->setColumnCount(3);
          this->resizeColumnsToContents();
          this->setSelectionBehavior(QAbstractItemView::SelectRows);
          this->horizontalHeader()->hide();
          this->setShowGrid(false);
          this->setStyleSheet("QTableView { border: 0px; }");
      
          mStatus.Undone = QIcon(":/icons/img/undone.png");
          mStatus.Pending = QIcon(":/icons/img/pending.png");
          mStatus.Done  = QIcon(":/icons/img/done.png");
      }
      
      void WeekPlanWidget::getTasks(QDate date)
      {
          mId.clear();
      
          Database *db = Database::instance();
          QVector<QList<QString>> data = db->selectTasks(date);
      
          int i = 0;
          for (QList<QString> row : data)
          {
              mId.append(row.at(0).toInt());
      
              QDateTime datetime = QDateTime::fromString(row.at(1), "yyyy-MM-dd hh:mm:ss");
              QTableWidgetItem *time = new QTableWidgetItem(datetime.time().toString("hh:mm"));
              QTableWidgetItem *task = new QTableWidgetItem(row.at(2));
              QTableWidgetItem *icon = new QTableWidgetItem();
      
              if (row.at(3).toInt() == 0)
                  icon->setIcon(mStatus.Pending);
              else if (row.at(3).toInt() == 1)
                  icon->setIcon(mStatus.Undone);
              else if (row.at(3).toInt() == 2)
                  icon->setIcon(mStatus.Done);
      
              this->setItem(i, 0, time);
              this->setItem(i, 1, task);
              this->setItem(i, 2, icon);
              i++;
          }
      }
      
      joeQJ Offline
      joeQJ Offline
      joeQ
      wrote on last edited by
      #2

      @Fuel hi,friend.welcome.

      From your snippet. i didn't see any error. could you show some information by below snippet:

      void WeekPlanWidget::getTasks(QDate date)
      {
          mId.clear();
      
          Database *db = Database::instance();
          QVector<QList<QString>> data = db->selectTasks(date);
      
          int i = 0;
          qDebug() << data.count(); ///
          for (QList<QString> row : data)
          {
              qDebug() << "strings : " << row; ///
              mId.append(row.at(0).toInt());
      
              QDateTime datetime = QDateTime::fromString(row.at(1), "yyyy-MM-dd hh:mm:ss");
              QTableWidgetItem *time = new QTableWidgetItem(datetime.time().toString("hh:mm"));
              QTableWidgetItem *task = new QTableWidgetItem(row.at(2));
              QTableWidgetItem *icon = new QTableWidgetItem();
      
              if (row.at(3).toInt() == 0)
                  icon->setIcon(mStatus.Pending);
              else if (row.at(3).toInt() == 1)
                  icon->setIcon(mStatus.Undone);
              else if (row.at(3).toInt() == 2)
                  icon->setIcon(mStatus.Done);
      
              this->setItem(i, 0, time);
              this->setItem(i, 1, task);
              this->setItem(i, 2, icon);
              qDebug("add row ok--%d",i);
              i++;
          }
      }
      

      Just do it!

      1 Reply Last reply
      2
      • F Offline
        F Offline
        Fuel
        wrote on last edited by Fuel
        #3

        @joeQ said in Own QTableWidget Class dont add Items:

        qDebug("add row ok--%d",i);

        The Output is

        add row ok--0
        add row ok--1

        so he find my 2 Entrys from my Database. The String of the Row are correct. He shows the Data.

        joeQJ 1 Reply Last reply
        0
        • F Fuel

          @joeQ said in Own QTableWidget Class dont add Items:

          qDebug("add row ok--%d",i);

          The Output is

          add row ok--0
          add row ok--1

          so he find my 2 Entrys from my Database. The String of the Row are correct. He shows the Data.

          joeQJ Offline
          joeQJ Offline
          joeQ
          wrote on last edited by
          #4

          @Fuel hi,

          ^-^!!!, i know where you wrong. you didn't write insertRow

          Here are my code from my project

          QTableWidgetItem *item[COLUMNS];
          
              int row = rowCount();
              insertRow(row); /// First to insert new row
          
              item[Name] = new QTableWidgetItem(QVariant::String);
              item[Name]->setFlags(item[Name]->flags() & ~Qt::ItemIsEditable);
              item[Name]->setText(codename);
              setItem(row,Name,item[Name]);
          
              item[Content] = new QTableWidgetItem(QVariant::String);
              item[Content]->setFlags(item[Content]->flags() & ~Qt::ItemIsEditable);
              item[Content]->setText(QString(codedata));
              item[Content]->setData(Qt::UserRole,QVariant(codedata));
              setItem(row,Content,item[Content]);
          
              item[Coordinate] = new QTableWidgetItem(QVariant::String);
              item[Coordinate]->setFlags(item[Coordinate]->flags() & ~Qt::ItemIsEditable);
              item[Coordinate]->setText(coornidate);
              setItem(row,Coordinate,item[Coordinate]);
          
              item[Operator] = new QTableWidgetItem(QVariant::Icon);
              item[Operator]->setFlags(item[Operator]->flags() & ~Qt::ItemIsEditable);
              setItem(row,Operator,item[Operator]);
          

          Just do it!

          1 Reply Last reply
          3
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            Post-solve comment. Given what you are doing, wouldn't a QSqlQueryModel+QTableView+a very simple QStyledItemDelegate for the icons be easier than this?

            "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
            1
            • F Offline
              F Offline
              Fuel
              wrote on last edited by
              #6

              Yes it worked thanks to all.

              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