【问题标题】:Strange behaviour of changing UI in QT在 QT 中更改 UI 的奇怪行为
【发布时间】:2014-12-09 09:49:08
【问题描述】:

我正在尝试修改 UI。但是我不明白某些功能我无法修改 UI。例如:

// In this function there is nothing wrong.
void FindUser::on_btnBrowse_clicked()
{

    browseFileName = QFileDialog::getOpenFileName(this, tr("Select Image"),
                                            "file:///.1/", tr("Image Files (*.png *.jpg *.bmp)"));

    qDebug() << browseFileName;

    if(browseFileName != "")
    {
        ui->btnFindPerson->setEnabled(true);

        selectedImg = QImage(browseFileName,"JPG");

        ui->lblFindPersonImage->setPixmap(QPixmap::fromImage(selectedImg));
        ui->lblFindPersonImage->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
        ui->lblFindPersonImage->setScaledContents(true);

    }
    else
    {
        ui->btnFindPerson->setEnabled(false);
    }
}
    // However in this function when some operation done. Ui is not changing. I am sure that userFound() is working because I can see output of qDebug() without any problem. And also I add some Qlabel, and also they are not changing too.
    void FindUser::userFound(QString imgFileName)
{


    QStringList imgName = imgFileName.split(".");

    qDebug() << imgName.at(0);

    QString resultImgFileName = "/.1/Projects/MGFaceApp/bin/db/visible/" + imgName.at(0) + ".jpg";

    QPixmap resultImgPixmap(resultImgFileName);

    qDebug() << resultImgFileName;

    ui->lblFindResultImage->setPixmap(resultImgPixmap);
    ui->lblFindResultImage->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    ui->lblFindResultImage->setScaledContents(true);
}

// 这里是userFound(),它被调用的地方。

bool ImageProcess::imgIdentfiy(QImage img_ident)
{
    ....
....
    ....
    if(identifyMember.identify(......))
    {
        ....

        if(matchedImgName != "")
        {
            FindUser findUserMember;
            findUserMember.userFound(matchedImgName);
        }
    }

....
        qDebug() << "Identify : DONE - DONE";
    return true;
}

这里还有头文件:

    namespace Ui {
class FindUser;
}

class FindUser : public QWidget
{
    Q_OBJECT

public:
    explicit FindUser(QWidget *parent = 0);
    ~FindUser();
    void userFound(QString imgFileName);

private slots:
    void on_btnBrowse_clicked();

    void on_btnFindPerson_clicked();


private:
    Ui::FindUser *ui;
    QString browseFileName;
    QImage selectedImg;
    QString resultImgFileName;

};

我正在尝试查找我应该检查哪个选项,但我的尝试都没有解决这个问题。你有什么想法找到这个吗?

已编辑:这是我的实际代码。

【问题讨论】:

  • 您确定实际调用了 userFound 方法吗? (它不是一个插槽)
  • @geoalgo,是的,我添加了 qDebug() 并查看控制台的输出。
  • 我很惊讶地发现通过赋值运算符设置标签的文本是可行的。我会期待像ui-&gt;lblPersonName-&gt;setText("..."); 这样的东西。这是哪个 Qt 版本?
  • @goGud 你不能直接复制粘贴你的代码而不是再次输入吗?给我们一个minimal working example
  • 当您的if 完成时,您的FindUser findUserMember 将超出范围。您确定要在此处创建FindUser 的新实例吗?我怀疑您已经在某处拥有FindUser 的现有实例,您可以在屏幕上看到它。

标签: c++ qt qlabel


【解决方案1】:

使用信号和槽在两个类之间进行通信。 在您的 ImageProcess 类中创建一个信号并将 FindUser::userFound 变成一个插槽。

signals:
    void userFound(const QString &imgName);

 

bool ImageProcess::imgIdentfiy(QImage img_ident)
{
    ....
        if(matchedImgName != "")
        {
            emit userFound(matchedImgName);
        }
...
}

并在您创建这些类的实例的地方连接信号和插槽:

FindUser *fUser = new FindUser(this);
ImageProcess *imgProcess = new ImageProcess(this);
connect(imgProcess, SIGNAL(userFound(QString)), fUser, SLOT(userFound(QString)));

我不知道您的应用程序是如何工作的,但请以此为指导。

【讨论】:

  • 您的解决方案似乎是正确的,但是我无法将其应用于我的代码。而且我只是将文件名传递给类并在 FindUser 中设置为 QLabel。为什么我要进行这样的信号/插槽连接。我认为应该没有必要..
  • @goGud 因为它简单易行。两个对象都不需要知道另一个对象。为什么你不能将它应用到你的代码中?
  • 我不明白应该在哪里创建 FindUser *fUserImageProcess *imgProcess.. 在 imageprocess.cpp 中?还有我应该在哪里写connect(imgProcess, Sıgnal.... )
  • @goGud 我不知道你应该在哪里创建它们,但你应该知道。你创造了它们。它们在您的代码中的某个位置。这只是一个例子。这就是为什么我在回答的最后说我不知道​​你的应用程序是如何工作的,你应该使用我的回答作为指导,而不仅仅是复制粘贴。我不可能知道您的应用程序的设计。你可以提供更多信息,比如你在哪里初始化你的FindUser对象和ImageProcess对象,我会相应地修改我的答案。
  • 正如你在我的问题中看到的,我在if statement 中初始化了FindUser,但你说你不应该这样做,因为它会消失if statement finish
【解决方案2】:

您是否检查了图像是否从磁盘正确加载?

可能路径不正确,或者文件损坏,...

您应该检查resultImgPixmap 是否确实包含图像数据。为此使用resultImgPixmap.isNull(),它应该返回falseresultImgPixmap.height()resultImgPixmap.width() 也应该返回大于零的值。

如果lblFindResultImage 在调用FindUser::userFound 之前没有先前的内容并且像素图加载失败,则将一个空像素图替换为另一个空像素图。显然在这种情况下 UI 不会改变。


另见thuga的评论,这也很有可能是问题的根源。

【讨论】:

  • 是的,它可以正确加载。我认为 thuga 的评论是正确的。但是我还没有找到解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-08
  • 1970-01-01
  • 2011-06-02
  • 1970-01-01
相关资源
最近更新 更多