Qt,如果单元格是小部件,如何对QTableWidget列进行排序 [英] Qt, How can I sort QTableWidget column if the cells are widgets

查看:404
本文介绍了Qt,如果单元格是小部件,如何对QTableWidget列进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个QTableWidget,有些列用文本填充,有些用数字填充,这些列可以很好地排序.但是我还有一列包含自定义小部件的列,如何启用这些小部件的排序功能?

I have a QTableWidget, some columns are filled with text, some with numbers, these columns are fine to sort. But I also have a column with custom widgets, how should I enable sorting for these?

我首先想到的当然是在QTableWidgetItem上重载'<'方法,但是没有QTableWidgetItem.那么解决这个问题的最佳方法是什么?

My first thought was of course to overload the '<' method on the QTableWidgetItem, but there are no QTableWidgetItem. So what would be the best way to solve this?

推荐答案

QTableWidget在这种情况下不理想,请考虑使用QTableView.无论如何,我将向您展示如何在QTableWidget中对QProgressBar小部件进行排序.

QTableWidget is not ideal for this case, consider using QTableView. Anyway, I will show you how to sort a QProgressBar widget in a QTableWidget.

正如您已经说过的,在这种情况下,您可以使<() operator超载.假设您在第4列中有一个QProgressBar,则必须重载<()运算符.

As you already said, you can overload the <() operator for this case. Let's say you have a QProgressBar in column number 4. You have to overload the <() operator.

您必须继承QProgressBarQTableWidgetItem的子类.

class CustomTableWidgetItem : public QProgressBar, public QTableWidgetItem
{
    Q_OBJECT
public:
    CustomTableWidgetItem( QWidget* parent ) : QProgressBar( parent )
    {
    }

    CustomTableWidgetItem(const QString txt = QString("0"))
        :QTableWidgetItem(txt)
    {
    }

    bool operator <(const QTableWidgetItem& other) const
    {
         if(other.column() == 0 /* numeric cell */) {
             return QTableWidgetItem::text().toInt() < other.text().toInt();
         }
         else if(other.column() == 4 /* progress bar */) {
            const QProgressBar *p = dynamic_cast<const QProgressBar *>(&other);
            if(p != 0) {
                if(this->value() < p->value())
                    return true;
            }
        }

        return false;
    }
};

然后您可以将QProgressBar这样插入到4号单元格中.

And then you can insert your QProgressBar like this in cell number 4.

像这样插入QProgressBar

ui->tableWidget->insertRow(ui->tableWidget->rowCount());
CustomTableWidgetItem *pgbar = new CustomTableWidgetItem(this);
ui->tableWidget->setCellWidget(ui->tableWidget->rowCount()-1, 4, pgbar);
ui->tableWidget->setItem(ui->tableWidget->rowCount()-1, 4, pgbar);

假设您要在单元格1处使用简单文本,请使用QTableWidgetItem.

Let's say you want to have simple text at cell 1 use QTableWidgetItem.

ui->tableWidget->setItem(ui->tableWidget->rowCount()-1, 1, new QTableWidgetItem("Hello"));

如果您还想对数字进行排序,例如在单元格0中,请使用CustomTableWidgetItem,因为我们已经实现了if语句,如上图所示,用于对数字和进度条进行排序.

If you want to sort numbers as well, for example in cell 0 use CustomTableWidgetItem, since we have implemented an if-statement as you can see above for sorting numbers and progressbar.

ui->tableWidget->setItem(ui->tableWidget->rowCount()-1, 0, new CustomTableWidgetItem(QString::number(ui->tableWidget->rowCount())));

您也可以将这种方法与其他窗口小部件一起使用,只是将正确的窗口小部件子类化,但是通常最好使用QTableView(MVC方法).

You can use this approach with other widgets as well, just subclass the correct widget, but in general it's better to use QTableView (MVC approach).

这是具有QTableWidgetQProgressBar排序的屏幕截图.

Here is a screenshot with QTableWidget and QProgressBar sorting.

这篇关于Qt,如果单元格是小部件,如何对QTableWidget列进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆