Qt-如何从QObject转换为UI元素? [英] Qt - How to convert from QObject to UI elements?

查看:74
本文介绍了Qt-如何从QObject转换为UI元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Qt 4.7中工作,并且对话框中有一个QWidget对象.我需要遍历每个孩子并将当前文本提取到QStringList中.其他每个对象都是QCheckBox,其余的都是QComboBoxes(我只需要两个的文本部分).到目前为止,我唯一想到的方法是使用children()函数将其作为QObject *并进行转换,如下所示:

I am working in Qt 4.7, and I have a QWidget object in my dialog. I need to go through each of the children and extract the current text into a QStringList. Every other object is a QCheckBox, and the rest are QComboBoxes (I would just need the text portion of both). So far the only way I could think of to do this would be to use the children() function to get them as QObject*'s and cast them, like this:

QStringList textlist;
for(int i = 0; i < ui->myWidget->children().size(); i++)
{
    if(i % 2 == 0)
    {
        QCheckBox *q = (QCheckBox *)ui->myWidget->children().at(i);
        textlist.append(q->text());
    }
    else
    {
        QComboBox *q = (QComboBox *)ui->myWidget->children().at(i);
        textlist.append(q->currentText());
    }
}

但是,当我尝试使用它时,它可以很好地构建和编译,但是在运行时会崩溃.我检查了两个类,它们都是QObject的子类(尽管是通过QAbstractButton和QWidget间接实现的),QObject是列表ui-> myWidget-> children()中对象的类型,所以我觉得它们应该可以通过这种方式进行转换.之前我在这种事情上工作不多,所以我不确定是否有更好的方法可以做到这一点.如果有人有任何想法,将不胜感激.谢谢!

However, when I try to use this, it builds and compiles fine, but then crashes when it's run. I checked and both classes are subclasses (albeit indirectly through QAbstractButton and QWidget) of QObject, which is the type of the objects in the list ui->myWidget->children(), so I feel like they should be able to cast this way. I haven't worked much with this kind of thing before so I'm not sure if there's a better way to do this. If anyone has any ideas, it would be greatly appreciated. Thanks!

更新:因此,我无法以这种方式或使用qobject_cast进行强制转换.但是我发现我可以从QObject转到QWidget,并且我认为我应该可以使用dynamic_cast从QWidget转到所需的对象,但这也不起作用.现在我有这个:

UPDATE: So, I can't get the casting to work this way or with the qobject_cast. I HAVE however discovered that I can go from QObject to QWidget, and I think I should be able to go from QWidget to the needed objects with dynamic_cast, but that doesn't work either. Right now I have this:

QStringList textlist;
for(int i = 0; i < ui->myWidget->children().size(); i++)
{
    QWidget *qw = qobject_cast<QWidget*>(ui->myWidget->children().at(i)
    if(i % 2 == 0)
    {
        QComboBox *q = dynamic_cast<QComboBox*>(qw);
        if(q)
        {
            textlist.append(q->text());
        }
    }
    else
    {
        QCheckBox *q = dynamic_cast<QCheckBox*>(qw);
        if(q)
        {
            textlist.append(q->currentText());
        }
    }
}

如果有人有任何想法,我将不胜感激.谢谢.

If anyone has any ideas, I'd appreciate the help. Thank you.

UPDATE2:我还没有在网上找到很多可以解决此问题的方法,所以我也可能会问,是否有某种方法可以不进行强制转换,即直接从QWidget以其原始类型获取对象,我也非常感谢.我对当前的策略或任何事情都不心存疑虑,这只是我可以想到的唯一方法-此时,我将采取一切可行的措施.

UPDATE2: I haven't found much online that helps with this still, so I may as well ask as well, if there is anyway to do this WITHOUT casting, i.e. getting the objects directly from the QWidget in their original type, I would really appreciate that as well. I'm not heartset on my current strategy or anything, it was just the only way I could think to do it - I'll take anything that works at this point.

推荐答案

您应该考虑使用 qobject_cast .强制转换之后,您还应该检查对象是否有效.

You should think about using qobject_cast. After the cast you should also check if the object is valid.

QStringList textlist;
for(int i = 0; i < ui->myWidget->children().size(); i++)
{
    if(i % 2 == 0)
    {
        QCheckBox *q = qobject_cast<QCheckBox*>(ui->myWidget->children().at(i));
        if(q)
           textlist.append(q->text());
    }
    else
    {
        QComboBox *q = qobject_cast<QComboBox*>(ui->myWidget->children().at(i));
        if(q)
           textlist.append(q->currentText());
    }
}

但是对于我来说,这仍然是一个不好的设计.包含组合框和复选框的窗口小部件类应具有一个函数,该函数遍历该复选框和组合框并返回 QStringList .然后,您可以调用该函数.

But this still seems like a bad design to me. The widget class that contains the comboboxes and checkboxes should have a function, that goes through the checkboxes and comboboxes and returns a QStringList. Then you could just call that function.

这是一个例子

mywidget.h:

mywidget.h:

namespace Ui {
class MyWidget;
}

class MyWidget : public QWidget
{
    Q_OBJECT

public:
    explicit MyWidget(QWidget *parent = 0);
    ~MyWidget();
    QStringList getComboTextList();
    QStringList getCheckBoxTextList();

private:
    Ui::MyWidget *ui;
    QList<QComboBox*> comboList;
    QList<QCheckBox*> checkList;
};

mywidget.cpp:

  mywidget.cpp:

MyWidget::MyWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MyWidget)
{
    ui->setupUi(this);
    this->setLayout(new QVBoxLayout);
    for(int i = 0; i < 5; i++)
    {
        QCheckBox *checkBox = new QCheckBox(this);
        this->layout()->addWidget(checkBox);
        checkBox->setText(QString("Check box #%1").arg(i));
        checkList.append(checkBox);
    }

    for(int i = 0; i < 5; i++)
    {
        QComboBox *comboBox = new QComboBox(this);
        this->layout()->addWidget(comboBox);
        comboBox->addItem("Combo box item 1");
        comboBox->addItem("Combo box item 2");
        comboList.append(comboBox);
    }
}

MyWidget::~MyWidget()
{
    delete ui;
}

QStringList MyWidget::getComboTextList()
{
    QStringList returnList;
    for(int i = 0; i < comboList.length(); i++)
    {
        returnList << comboList[i]->currentText();
    }
    return returnList;
}

QStringList MyWidget::getCheckBoxTextList()
{
    QStringList returnList;
    for(int i = 0; i < checkList.length(); i++)
    {
        returnList << checkList[i]->text();
    }
    return returnList;
}

然后在其他课程中,您可以像这样调用 getCheckBoxTextList getComboTextList :

Then in your other class you can just call getCheckBoxTextList or getComboTextList like this:

QStringList comboTextList = myWidget->getComboBoxList();
QStringList checkTextList = myWidget->getCheckBoxTextList();

这篇关于Qt-如何从QObject转换为UI元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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