Own QTableWidget Class dont add Items



  • 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++;
        }
    }
    


  • @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++;
        }
    }
    


  • @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.



  • @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]);
    


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



  • Yes it worked thanks to all.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.