Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ QT中改变UI的奇怪行为_C++_Qt_Qlabel - Fatal编程技术网

C++ QT中改变UI的奇怪行为

C++ QT中改变UI的奇怪行为,c++,qt,qlabel,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

我正在尝试修改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);
}
我试图找到我应该检查的选项,但我的任何尝试都没有解决这个问题。你知道怎么找到这个吗


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

您是否检查了映像是否正确地从磁盘加载

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

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

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



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

使用信号和插槽在两个类之间进行通信。 在
ImageProcess
类中创建一个信号,并将
FindUser::userFound
转换为插槽

signals:
    void userFound(const QString &imgName);

并将信号和插槽连接到创建这些类实例的位置:

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

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

您确定实际调用了userFound方法吗?(它不是插槽)@geoalgo,是的,我添加了qDebug()并查看控制台的输出。我惊讶地发现通过赋值操作符设置标签的文本是有效的。我会期望像
ui->lblPersonName->setText(“…”)这样的东西。这是哪个Qt版本?@goGud你不能复制并粘贴你的代码,而不是再次键入它吗?给我们一口井,当您的
if
完成时,您的
FindUser findUserMember
将超出范围。是否确实要在此处创建
FindUser
的新实例?我怀疑你已经有了一个
FindUser
的实例,你可以在屏幕上看到它。是的,它可以正确加载。我认为图加的评论是正确的。然而,我还没有找到解决方案。您的解决方案似乎是正确的,但我无法将其应用到我的代码中。而且我只是将文件名传递给类,并在FindUser中设置为QLabel。为什么我应该进行这样的信号/插槽连接。我认为没有必要……@goGud,因为它简单易行。两个对象都不需要知道另一个对象。为什么不能将其应用到代码中?我不知道应该在哪里创建
FindUser*fUser
ImageProcess*imgProcess
。。在
imageprocess.cpp
中?还有我应该在哪里写
connect(imgProcess,Sıgnal…)
?@goGud我不知道应该在哪里创建它们,但你应该知道。你已经创造了它们。它们在代码中的某个地方。这只是一个例子。这就是为什么我在回答的最后说,我不知道你的应用程序是如何工作的,你应该用我的答案作为指导,而不仅仅是复制粘贴它。我不可能知道你的应用程序的设计。您可以提供更多信息,例如在何处初始化
FindUser
对象和
ImageProcess
对象,我将相应地修改我的答案。正如您在我的问题中看到的,我在
if语句中初始化
FindUser
,但您说过不应该这样做,因为如果语句完成,它将退出

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)));