检索作为 QLineEdit 小部件的 QTableWidget 单元格的值 [英] Retrieve value of QTableWidget cells which are QLineEdit widgets

查看:106
本文介绍了检索作为 QLineEdit 小部件的 QTableWidget 单元格的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 QLineEdit,设置了一个验证器并使用以下代码将其放在表上:

I create a QLineEdit,set a validator and put it on the table with this code:

ui->moneyTableWidget->setCellWidget(rowsNum, 1, newQLineEdit);

然后我有另一个类来操作表的数据,对列的每个值求和.代码如下:

Then I've got another class for manipulating the table's data doing the sum of every value of a column. Here's the code:

int Calculator::calculatePricesSum(QTableWidget &moneyTableWidget){
    double total = 0;
    QWidget *tmpLineEdit;
    QString *tmpString;
    for(int row=0; row<moneyTableWidget.rowCount(); row++){
        tmpLineEdit = (QLineEdit*)moneyTableWidget.cellWidget(row,1);       
        tmpString = tmpLineEdit.text();
        total += tmpString->toDouble();
    }
    return total;
}

但是构建失败并出现此错误:

But the building fails with this error:

/home/testpec/src/nokiaQT/MoneyTracker-build-simulator/../MoneyTracker/calculator.cpp:11:错误:无法将QLineEdit*"转换为赋值中的‘QWidget*’

/home/testpec/src/nokia QT/MoneyTracker-build-simulator/../MoneyTracker/calculator.cpp:11: error: cannot convert ‘QLineEdit*’ to ‘QWidget*’ in assignment

为什么会出现这种转换错误?

Why this convertion error?

另一个子问题:将表作为引用传递可以节省内存吗?这可能是问题吗?我正在为诺基亚智能手机开发,我认为按值传递对象是浪费内存......(对不起,如果这是一个愚蠢的问题,但我对 C++ 和所有指针的东西有点生疏......)

Another subquestion: passing the table as reference saves memory right? Could this be the problem? Im developing for a Nokia smartphone and I think passing the object by value is a waste of memory...(sorry if is a dumb question but I'm a little rusty with C++ and all the pointers stuff...)

推荐答案

当您声明 tmpLineEdit 时,您应该将其声明为 QLineEdit* 而不是 <代码>QWidget*.您的循环获取小部件,将其转换为 QLineEdit*,然后尝试将其放回 QWidget*.另外,我建议您使用 qobject_cast(或 dynamic_cast),这样您就可以确保转换成功.

When you declare your tmpLineEdit, you should be declaring it as a QLineEdit* instead of a QWidget*. Your loop grabs the widget, casts it to a QLineEdit* and then tries to put it back into a QWidget*. Also, I'd recommend using qobject_cast<QLineEdit*> (or dynamic_cast) so that you can ensure the cast succeeded.

int Calculator::calculatePricesSum(QTableWidget &moneyTableWidget){
    double total = 0;
    QLineEdit* tmpLineEdit;
    QString tmpString;
    for(int row=0; row < moneyTableWidget.rowCount(); row++)
    {
        tmpLineEdit = qobject_cast<QLineEdit*>(moneyTableWidget.cellWidget(row,1));
        if(NULL == tmpLineEdit)
        {
            // Do something to indicate failure.
        }
        tmpString = tmpLineEdit->text();
        total += tmpString.toDouble();
    }
    return total;
}

至于你的第二个问题,通过引用传递可能是一个好主意——我知道 Qt 中的一些类(特别是 QImage)使用引用计数和隐式共享,这样你就可以通过值传递而不必担心影响大型复制操作,但我不确定 QTableWidget 是否也属于该类别.

As for your second question, passing by reference is probably a good idea - I know some of the classes in Qt (QImage in particular) use reference counting and implicit sharing so that you can pass around by value without worrying about the implications of large copy operations, but I'm not sure if a QTableWidget is in that category as well.

这篇关于检索作为 QLineEdit 小部件的 QTableWidget 单元格的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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