QT:清除QTableWidget以显示新项目

时间:2016-05-20 20:25:54

标签: qt qtablewidget

我正在创建一个程序,向QTableWidget显示客户列表。在单击其中一个按钮之前,该表不会输出。每个按钮都会输出正确的项目,但是,例如,当我点击“关键客户”时,“全部显示”表格不会将新项目更改/输出到表格。

-

我希望表格清除其项目,然后在同一个表格小部件上重新显示新项目。

-

之前我有这个工作,但没有保存回购的更改。

This is how the window is displayed.

广告代码:

///
/// \brief AdminWindow::on_showAll_Button_clicked
/// Displays a QTableWidget populated by all items in the customer list
///

void AdminWindow::on_showAll_Button_clicked()
{
QStringList header;
Customer* ptr=NULL;     /// \brief points to the current obj being output
int row=0;              /// \brief current row
int col=0;              /// \brief current col

ptr=CList.list.returnObj();

ui->tableWidget->reset();

header << "Name" << "Street Address" << "City Address" << "Rating" <<
          "Priority";
ui->tableWidget->setHorizontalHeaderLabels(header);

while(ptr!=NULL)
{
    ui->tableWidget->setRowCount(row+1);
    ui->tableWidget->setColumnCount(col);

    col=0;
    QTableWidgetItem* newName = new QTableWidgetItem
            (ptr->getName());
    ui->tableWidget->setItem(row-1, col, newName);

    col=1;
    QTableWidgetItem* newStrtAd = new QTableWidgetItem
            (ptr->getStreetAd());
    ui->tableWidget->setItem(row-1, col, newStrtAd);

    col=2;
    QTableWidgetItem* newCtyAd = new QTableWidgetItem
            (ptr->getCityAd());
    ui->tableWidget->setItem(row-1, col, newCtyAd);

    col=3;
    QTableWidgetItem* newRate = new QTableWidgetItem
            (ptr->getRating());
    ui->tableWidget->setItem(row-1, col, newRate);

    col=4;
    QTableWidgetItem* newKey = new QTableWidgetItem
            (ptr->getKeyCust());
    ui->tableWidget->setItem(row-1, col, newKey);

    CList.list.getNext();
    ptr=CList.list.returnObj();
    row++;
    col++;
}
}

///
/// \brief AdminWindow::on_KeyButton_clicked
/// Displays a QTableWidget populated by the customer list and filtered
///     to display Key Customers
///

void AdminWindow::on_KeyButton_clicked()
{
QStringList header;
Customer* ptr=NULL;     /// \brief points to the current obj being output
int row=0;              /// \brief current row
int col=0;              /// \brief current col

ptr=CList.list.returnObj();

ui->tableWidget->reset();

header << "Name" << "Street Address" << "City Address" << "Rating" <<
          "Priority";
ui->tableWidget->setHorizontalHeaderLabels(header);

while(ptr!=NULL)
{
    if(ptr->getKeyCust()=="key")
    {
        ui->tableWidget->setRowCount(row+1);
        ui->tableWidget->setColumnCount(col);

        col=0;
        QTableWidgetItem* newName = new QTableWidgetItem
                (ptr->getName());
        ui->tableWidget->setItem(row-1, col, newName);

        col=1;
        QTableWidgetItem* newStrtAd = new QTableWidgetItem
                (ptr->getStreetAd());
        ui->tableWidget->setItem(row-1, col, newStrtAd);

        col=2;
        QTableWidgetItem* newCtyAd = new QTableWidgetItem
                (ptr->getCityAd());
        ui->tableWidget->setItem(row-1, col, newCtyAd);

        col=3;
        QTableWidgetItem* newRate = new QTableWidgetItem
                (ptr->getRating());
        ui->tableWidget->setItem(row-1, col, newRate);

        col=4;
        QTableWidgetItem* newKey = new QTableWidgetItem
                (ptr->getKeyCust());
        ui->tableWidget->setItem(row-1, col, newKey);

        CList.list.getNext();
        ptr=CList.list.returnObj();
        row++;
        col++;
    }else
    {
        CList.list.getNext();
        ptr=CList.list.returnObj();
    }
}
}

///
/// \brief AdminWindow::on_NonKeyButton_clicked -
/// Displays a QTableWidget populated by the customer list and filtered
///     to display only Non-Key customers
///
void AdminWindow::on_NonKeyButton_clicked()
{
QStringList header;
Customer* ptr=NULL;     /// \brief points to the current obj being output
int row=0;              /// \brief current row
int col=0;              /// \brief current col

ptr=CList.list.returnObj();

ui->tableWidget->reset();

header << "Name" << "Street Address" << "City Address" << "Rating" <<
          "Priority";
ui->tableWidget->setHorizontalHeaderLabels(header);

while(ptr!=NULL)
{
    if(ptr->getKeyCust()!="key")
    {
        ui->tableWidget->setRowCount(row+1);
        ui->tableWidget->setColumnCount(col);

        col=0;
        QTableWidgetItem* newName = new QTableWidgetItem
                (ptr->getName());
        ui->tableWidget->setItem(row-1, col, newName);

        col=1;
        QTableWidgetItem* newStrtAd = new QTableWidgetItem
                (ptr->getStreetAd());
        ui->tableWidget->setItem(row-1, col, newStrtAd);

        col=2;
        QTableWidgetItem* newCtyAd = new QTableWidgetItem
                (ptr->getCityAd());
        ui->tableWidget->setItem(row-1, col, newCtyAd);

        col=3;
        QTableWidgetItem* newRate = new QTableWidgetItem
                (ptr->getRating());
        ui->tableWidget->setItem(row-1, col, newRate);

        col=4;
        QTableWidgetItem* newKey = new QTableWidgetItem
                (ptr->getKeyCust());
        ui->tableWidget->setItem(row-1, col, newKey);

        CList.list.getNext();
        ptr=CList.list.returnObj();
        row++;
        col++;
    }else
    {
        CList.list.getNext();
        ptr=CList.list.returnObj();
    }
}
}

我已经尝试了所有ui-&gt; tableWidget-&gt; clear ..()和ui-&gt; tableWidget-&gt; reset()方法无济于事。感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我相信您正在寻找tableWidget->setRowCount(0)。此功能清除表格,但不清除标题标签。我使用它并且它比clearreset方法效果更好。