Qt系列文章目录
文章目录
- Qt系列文章目录
- 前言
- 一、QtCreator中qmake命令是什么?
- 2.解决
前言
我在代码中加入了对应的信号和槽,但编译仍然报错:
#ifndef PROJECTWIN_H
#define PROJECTWIN_H
namespace Ui {
class ProjectWin;
}
ProjectWin类声明头文件
class ProjectWin : public QWidget
{
Q_OBJECT
public:
static ProjectWin* getInstance();
~ProjectWin();
FileMonitorMgr *m_fileMgr;
QString m_paraFolder;
private:
QTreeWidget* m_picTree;
// QTreeWidget* m_paraTree;
QWidget* m_naviWgt;
private:
void initWidget();
void initPicTree();
void readParaFile(QString filePath);
private:
QTextEdit* m_paraText;
static ProjectWin* m_pInstance;
private:
explicit ProjectWin(QWidget *parent = nullptr);
static void destroyInstance();
public Q_SLOTS:
void slotParaInfo(QString imageName);
};
#endif // PROJECTWIN_H
ProjectWin类实现
#include "ProjectWin.h"
#include "ui_ProjectWin.h"
#pragma execution_character_set("utf-8")
ProjectWin* ProjectWin::m_pInstance = nullptr;
ProjectWin* ProjectWin::getInstance()
{
if(!m_pInstance)
{
m_pInstance = new ProjectWin();
atexit(destroyInstance);
}
return m_pInstance;
}
void ProjectWin::destroyInstance()
{
if(m_pInstance)
{
delete m_pInstance;
m_pInstance = nullptr;
}
}
ProjectWin::~ProjectWin()
{
delete ui;
}
ProjectWin::ProjectWin(QWidget *parent) :
QWidget(parent),
ui(new Ui::ProjectWin)
{
ui->setupUi(this);
//图像参数
m_tabWidgetPara = new QTabWidget();
m_paraText = new QTextEdit();
m_tabWidgetPara->addTab(m_paraText, u8"图像参数数据");
m_tabWidgetPara->setStyleSheet("border: 0");
m_paraText->setStyleSheet("border: 0");
// m_tabWidgetPara->setTabText(0, u8"图像参数数据");
// 设置窗口的主布局
this->setLayout(mainLayout);
}
void ProjectWin::onItemClicked(QTreeWidgetItem *item, int column)
{
QString imageName = item->text(0);
emit sigShowImageBorder(imageName);
}
void ProjectWin::initWidget()
{
// m_picTree->header()->hide();//设置隐藏头
}
void ProjectWin::slotParaInfo(QString imageName)
{
QString fullPath = m_paraFolder + "/";
QDir directory(m_paraFolder);
QStringList filters;
filters << "*.txt";
directory.setNameFilters(filters);
directory.setFilter(QDir::Files | QDir::NoDotAndDotDot);
QFileInfoList fileInfoList = directory.entryInfoList();
for (const QFileInfo &fileInfo : fileInfoList) {
// qDebug() << "Found:" << fileInfo.absoluteFilePath();
QString fullPath = fileInfo.absoluteFilePath();
if(imageName == fileInfo.fileName())
{
fullPath += imageName;
readParaFile(m_paraFolder);
}
}
}
GraphicsView类实现实现
#ifndef GRAPHICSVIEW_H
#define GRAPHICSVIEW_H
#include <QtWidgets>
#include <QGraphicsView>
#include "GraphicsScene.h"
#include "GraphicsItem.h"
//#include <QGraphicViewMouseEvent>
class GraphicsScene;
class GraphicsView : public QGraphicsView
{
Q_OBJECT
public:
GraphicsView(QWidget *parent = nullptr)
{
}
protected:
void mousePressEvent(QMouseEvent *event) override
{
if (event->button() == Qt::LeftButton && event->modifiers() == Qt::ControlModifier) {
m_isMousePressed = true;
m_lastMousePos = event->pos();
setCursor(Qt::ClosedHandCursor);
} else {
if (event->button() == Qt::LeftButton) {
QGraphicsItem *item = itemAt(event->pos());
if(item)
{
GraphicsItem* selectedItem = dynamic_cast<GraphicsItem*>(item);
if(selectedItem)
{
QString imageName = selectedItem->getImageName();
emit imageNameSelected(imageName);
}
}
setDragMode(RubberBandDrag);
pressPosition = event->pos();
m_startScenePos = mapToScene(event->pos());
m_rubberBandDragging = true;
m_multipleItemsSelected = (scene()->selectedItems().size() > 1);
}
QGraphicsView::mousePressEvent(event);
}
}
private:
Q_SIGNALS:
void imageNameSelected(QString imageName);
};
#endif // GRAPHICSVIEW_H
ImageManager类定义
#ifndef IMAGEMANAGER_H
#define IMAGEMANAGER_H
#include <QWidget>
#include <QVector>
#include <QImage>
#include <QVBoxLayout>
#include "GraphicsItem.h"
#include "GraphicsView.h"
#include "GraphicsScene.h"
#include "ProjectWin.h"
class ImageManager : public QWidget
{
Q_OBJECT
public:
explicit ImageManager(/*ProjectWin* projectWin,*/QWidget *parent = nullptr);
private:
GraphicsScene* m_scene;
GraphicsView* m_view;
GraphicsItem* m_item;
QVector<GraphicsItem*> m_items;
QVBoxLayout* m_layout;
QString m_watchPath;
// ProjectWin* m_projectWin;
public Q_SLOTS:
void setImagePosition(QVector<QString> &filesVec, QString &path, int groupType);
Q_SIGNALS:
};
#endif // IMAGEMANAGER_H
#include "ImageManager.h"
#include "ProjectWin.h"
#include <QMetaObject>
ImageManager::ImageManager(/*ProjectWin* projectWin,*/QWidget* parent)
: QWidget(parent)/*, m_projectWin(projectWin)*//*, m_scene(new GraphicsScene(this)), m_view(new GraphicsView()), m_layout(new QVBoxLayout(this))*/ {
m_scene = new GraphicsScene(this);
m_view = new GraphicsView;
m_layout = new QVBoxLayout(this);
m_layout->addWidget(m_view);
setLayout(m_layout);
int winWidth = 2048; //1642
int winHeight = 1050; //692
// setFixedSize(winWidth, winHeight);
resize(winWidth, winHeight);
connect(m_view, &GraphicsView::imageNameSelected, ProjectWin::getInstance(), &ProjectWin::slotParaInfo);
}
ImageManager.obj👎 error: LNK2001: 无法解析的外部符号 "public: virtual struct QMetaObject const * __cdecl GraphicsView::metaObject(void)const " (?metaObject@GraphicsView@@UEBAPEBUQMetaObject@@XZ)
ImageManager.obj👎 error: LNK2001: 无法解析的外部符号 “public: virtual void * __cdecl GraphicsView::qt_metacast(char const *)” (?qt_metacast@GraphicsView@@UEAAPEAXPEBD@Z)
ImageManager.obj👎 error: LNK2001: 无法解析的外部符号 “public: virtual int __cdecl GraphicsView::qt_metacall(enum QMetaObject::Call,int,void * *)” (?qt_metacall@GraphicsView@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z)
ImageManager.obj👎 error: LNK2019: 无法解析的外部符号 “public: void __cdecl GraphicsView::imageNameSelected(class QString)” (?imageNameSelected@GraphicsView@@QEAAXVQString@@@Z),函数 “public: __cdecl ImageManager::ImageManager(class QWidget *)” (??0ImageManager@@QEAA@PEAVQWidget@@@Z) 中引用了该符号
debug\ImageManageSys.exe👎 error: LNK1120: 5 个无法解析的外部命令
一、QtCreator中qmake命令是什么?
qmake是一个跨平台的项目文件生成工具,它由Qt开发并维护。它的主要任务是读取你的项目文件(通常是.pro文件),并生成相应的Makefile或其他构建系统的配置文件。这些生成的配置文件可以被make或其他构建工具用来编译和链接你的Qt应用程序或库。
主要的作用如下:
生成构建配置文件:qmake读取.pro项目文件,生成对应平台或编译系统的构建配置文件(如Makefile,或者其他如Visual Studio项目文件)。
处理Qt特有的构建任务:qmake能处理Qt的一些特有的构建任务,如处理Qt的资源文件(.qrc)、用户界面文件(.ui)和处理为了实现Qt元对象系统而需要的moc(元对象编译器)文件。
跨平台兼容性:qmake可以生成不同平台和不同编译系统的构建配置文件,这使得Qt能够跨平台使用。你可以在一个平台上编写.pro文件,然后在其他平台上使用qmake来生成对应的构建配置文件。
使用qmake的基本步骤通常是:
编写一个.pro文件,描述项目的源文件、头文件、资源文件、需要的Qt模块等信息。
在命令行中运行qmake命令,根据.pro文件生成构建配置文件(如Makefile)。
运行make(或其他编译工具,如nmake、jom等)来编译和链接程序。
在Qt Creator中,这些步骤通常会被自动执行。当你修改.pro文件或者需要重新生成构建配置文件时,可以在Qt Creator中手动运行qmake。