在QT中更改UI的奇怪行为

时间:2014-12-09 09:49:09

标签: c++ qt qlabel

我正在尝试修改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;

};

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

EDITTED:这是我的实际代码。

2 个答案:

答案 0 :(得分:2)

使用信号和插槽在两个班级之间进行通信。 在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)));

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

答案 1 :(得分:0)

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

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

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

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


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