qt - 如何访问在 QtCreator Designer 中创建的 QLabel?

标签 qt opencv qt-creator

我使用 Qt Creator 和 opencv 在我的 MainWindow 中创建了一些 QLabel,但是我有这个错误:

error: 'ui' was not declared in this scope

在函数 filter_image() 中。我想在做一些处理之前和之后使用标签来显示图像。

主窗口.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include<QImage>
#include<QLabel>

static QImage IplImage2QImage(const IplImage *iplImage) {
    int height = iplImage->height;
    int width = iplImage->width;
    if  (iplImage->depth == IPL_DEPTH_8U && iplImage->nChannels == 3)
    {
        const uchar *qImageBuffer = (const uchar*)iplImage->imageData;
        QImage img(qImageBuffer, width, height, QImage::Format_RGB888);
        return img.rgbSwapped();
    }
    else if  (iplImage->depth == IPL_DEPTH_8U && iplImage->nChannels == 1)
    {
        const uchar *qImageBuffer = (const uchar*)iplImage->imageData;
        QImage img(qImageBuffer, width, height, QImage::Format_Indexed8);
        QVector<QRgb> colorTable;
        for (int i = 0; i < 256; i++)
        {
            colorTable.push_back(qRgb(i, i, i));
        }
        img.setColorTable(colorTable);
        return img;
    }
    else
    {
        std::cout << "Image cannot be converted.";
        return QImage();
    }
}

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

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

void MainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void filter_image(IplImage* img) {
    IplImage* roi = cvCreateImage(cvGetSize(img), 8, 3);
    cvCvtColor( img, roi, CV_RGB2GRAY );
    cvSmooth(img,roi,CV_BLUR, 5, 0, 0, 0);
    cvThreshold(roi,roi,100,255,CV_THRESH_BINARY);
    IplImage *img_de = cvCloneImage(roi);
    QImage qt_i = IplImage2QImage(img_de);
    QLabel label_2;
    // display on label
    ui->label_2->setPixmap(QPixmap::fromImage(qt_i));//error line
    // resize the label to fit the image
    ui->label_2->resize(ui->label_2->pixmap()->size());
    label_2.show();
}

void MainWindow::on_actionOpen_triggered() {
    IplImage *frame = cvLoadImage(
        QFileDialog::getOpenFileName(this, 
                                     "Ouvrir un fichier", 
                                     "/../../Fichiers Image", 
                                     "Image (*.jpg *.bmp *.jpeg)")
                                     .toStdString().c_str(),3);
    IplImage *img_des = cvCloneImage(frame);
    QImage qt_im = IplImage2QImage(img_des);
    QLabel label;
    // display on label
    ui->label->setPixmap(QPixmap::fromImage(qt_im));
    // resize the label to fit the image
    ui->label->resize(ui->label->pixmap()->size());
    label.show();
    filter_image(frame);
}

主窗口.h

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow {
    Q_OBJECT

public:    
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

protected:
    void changeEvent(QEvent *e);

public:
    Ui::MainWindow *ui;

public slots:
    void on_actionOpen_triggered();    
}; 

ui_mainwindow.h

#include <QtCore/QVariant>

#include <QtGui/QAction>

#include <QtGui/QApplication>

#include <QtGui/QButtonGroup>

#include <QtGui/QHeaderView>

#include <QtGui/QLabel>

#include <QtGui/QMainWindow>

#include <QtGui/QMenu>

#include <QtGui/QMenuBar>

#include <QtGui/QStatusBar>

#include <QtGui/QToolBar>

#include <QtGui/QWidget>

QT_BEGIN_NAMESPACE

class Ui_MainWindow{


public:
QAction *actionOpen;
QWidget *centralWidget;
QLabel *label;
QLabel *label_2;
QMenuBar *menuBar;
QMenu *menuFile;
QToolBar *mainToolBar;
QStatusBar *statusBar;

void setupUi(QMainWindow *MainWindow)
{
    if (MainWindow->objectName().isEmpty())
        MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
    MainWindow->resize(600, 400);
    actionOpen = new QAction(MainWindow);
    actionOpen->setObjectName(QString::fromUtf8("actionOpen"));
    centralWidget = new QWidget(MainWindow);
    centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
    label = new QLabel(centralWidget);
    label->setObjectName(QString::fromUtf8("label"));
    label->setGeometry(QRect(290, 30, 271, 301));
    label_2 = new QLabel(centralWidget);
    label_2->setObjectName(QString::fromUtf8("label_2"));
    label_2->setGeometry(QRect(20, 20, 281, 331));
    MainWindow->setCentralWidget(centralWidget);
    menuBar = new QMenuBar(MainWindow);
    menuBar->setObjectName(QString::fromUtf8("menuBar"));
    menuBar->setGeometry(QRect(0, 0, 600, 21));
    menuFile = new QMenu(menuBar);
    menuFile->setObjectName(QString::fromUtf8("menuFile"));
    MainWindow->setMenuBar(menuBar);
    mainToolBar = new QToolBar(MainWindow);
    mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
    MainWindow->addToolBar(Qt::TopToolBarArea, mainToolBar);
    statusBar = new QStatusBar(MainWindow);
    statusBar->setObjectName(QString::fromUtf8("statusBar"));
    MainWindow->setStatusBar(statusBar);

    menuBar->addAction(menuFile->menuAction());
    menuFile->addAction(actionOpen);

    retranslateUi(MainWindow);

    QMetaObject::connectSlotsByName(MainWindow);
} // setupUi

void retranslateUi(QMainWindow *MainWindow)
{
    MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
    actionOpen->setText(QApplication::translate("MainWindow", "Open", 0, QApplication::UnicodeUTF8));
    label->setText(QString());
    label_2->setText(QString());
    menuFile->setTitle(QApplication::translate("MainWindow", "File", 0, QApplication::UnicodeUTF8));
} // retranslateUi};



namespace Ui {
class MainWindow: public Ui_MainWindow {};

} // namespace Ui

最佳答案

发生错误的方法 filter_image 不是 MainWindow 的类方法,因此它无法访问 MainWindow 的成员(包括 ui)。

你可以做几件事:如果有意义,让它成为 MainWindow 类的一部分。或者,您可以将 ui 作为参数传递:

void filter_image(Ui::MainWindow* ui, /*other params*/){...}

此外,您正在创建一个局部变量 label_2 而不是从 gui 中获取。

关于qt - 如何访问在 QtCreator Designer 中创建的 QLabel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10341256/

相关文章:

c++ - 如何从 QString 获取 const char *

c++ - 没有子类化的 QOpenGLWidget

android - 在Windows for Android上从源代码构建Qt5.13.2时出错

qt - 如何将 QML 代码拆分为多个文件?

c++ - QFile::copy 是否保留复制文件中的源文件权限?

c++ - 图像导出在opencv中返回灰度图像

opencv - 如何修改openCV源代码?

python - cv2.remap 或 scipy.interpolate.map_coordinates 在 Tensorflow 中的等效/实现?

c++ - 突出显示 QTableWidget 的整行

qt - 如何在基于 Qt Widget 的应用程序中使用 DEPLOYMENTFOLDERS 指令