如何访问在QtCreator Designer中创建的QLabel?
我使用Qt Creator和opencv在主窗口中创建了一些如何访问在QtCreator Designer中创建的QLabel?,qt,opencv,qt-creator,Qt,Opencv,Qt Creator,我使用Qt Creator和opencv在主窗口中创建了一些QLabel,但我有以下错误: 错误:“ui”未在此作用域中声明 在函数filter\u image()中。我想使用标签来显示图像之前和之后做一些处理 mainwindow.cpp #include "mainwindow.h" #include "ui_mainwindow.h" #include <cv.h> #include <cxcore.h> #include <highgui.h> #in
QLabel
,但我有以下错误:
错误:“ui”未在此作用域中声明
在函数filter\u image()
中。我想使用标签来显示图像之前和之后做一些处理
mainwindow.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);
}
ui_main window.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();
};
#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
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
QT_开始名称空间
类Ui_主窗口{
公众:
QAction*actionOpen;
QWidget*centralWidget;
QLabel*标签;
QLabel*标签2;
QMenuBar*菜单栏;
QMenu*菜单文件;
QToolBar*主工具栏;
QStatusBar*状态栏;
无效设置UI(QMainWindow*MainWindow)
{
如果(MainWindow->objectName().isEmpty())
MainWindow->setObjectName(QString::fromUtf8(“MainWindow”);
主窗口->调整大小(600400);
actionOpen=新QAction(主窗口);
actionOpen->setObjectName(QString::fromUtf8(“actionOpen”);
centralWidget=新的QWidget(主窗口);
centralWidget->setObjectName(QString::fromUtf8(“centralWidget”);
标签=新的QLabel(centralWidget);
标签->设置对象名(QString::fromUtf8(“标签”);
标签->设置几何(QRect(290,30271301));
label_2=新的QLabel(centralWidget);
label_2->setObjectName(QString::fromUtf8(“label_2”);
label_2->setGeometry(QRect(20,20281331));
主窗口->设置中心Widget(中心Widget);
菜单栏=新的QMenuBar(主窗口);
菜单栏->设置对象名(QString::fromUtf8(“菜单栏”);
菜单栏->设置几何(QRect(0,0600,21));
menuFile=新的QMenu(菜单栏);
menuFile->setObjectName(QString::fromUtf8(“menuFile”);
主窗口->设置菜单栏(菜单栏);
mainToolBar=新的QToolBar(主窗口);
mainToolBar->setObjectName(QString::fromUtf8(“mainToolBar”);
主窗口->添加工具栏(Qt::TopToolbar区域,主工具栏);
statusBar=新的QStatusBar(主窗口);
statusBar->setObjectName(QString::fromUtf8(“statusBar”);
主窗口->设置状态栏(状态栏);
菜单栏->添加操作(菜单文件->菜单操作());
菜单文件->添加操作(操作打开);
重传(主窗口);
QMetaObject::connectSlotsByName(主窗口);
}//setupUi
无效重新传输(QMainWindow*MainWindow)
{
MainWindow->setWindowTitle(QApplication::translate(“MainWindow”,“MainWindow”,0,QApplication::UnicodeTf8));
actionOpen->setText(QApplication::translate(“主窗口”,“打开”,0,QApplication::UnicodeTf8));
标签->设置文本(QString());
标签2->setText(QString());
menuFile->setTitle(QApplication::translate(“主窗口”,“文件”,0,QApplication::UnicodeUTF8));
}//重新转换};
名称空间用户界面{
类主窗口:公共Ui_主窗口{};
}//名称空间用户界面
发生错误的方法filter\u image
,不是MainWindow
的类方法,因此它无法访问MainWindow
的成员(包括ui
)
您可以做几件事:如果有意义,将其作为main窗口
类的一部分。或者,您可以将ui作为参数传递:
void filter_image(Ui::MainWindow* ui, /*other params*/){...}
此外,您正在创建一个局部变量
label_2
,而不是从gui获取该变量。哪一行导致了错误?另外,更好地格式化代码也会有很大帮助。C:/Users/nadia/Desktop/Qt_-app/app/mainwindow.cpp:108:错误:“ui”未在此范围内声明我有此错误C:/Users/nadia/Desktop/Qt_-app/app/mainwindow.cpp:142:错误:无法将参数“1”的“IplImage*”转换为“ui::mainwindow*”,将参数“void filter\u-image(ui::mainwindow*,IplImage*)是的,用filter_image(ui,IplImage*)调用它,因为现在它需要两个参数。还要注意的是,这只是一个hack,所以你可以测试它。更聪明的方法是从filter_image方法返回一个QImage
,这样函数就不再需要了解ui。从主窗口调用它,获取图像,然后显示它。我认为它正在成功编译,然后。这意味着你正在进步。您是否尝试过打印调试消息以确定它在何处崩溃?此外,正如我在回答中指出的,您的本地QLabel标签可能是问题的根源-它毕竟不是gui的一部分,它只是一个局部变量。当我尝试添加Ui::MainWindow*Ui时,它可以工作,但我有一个其他小窗口