QT #pragma once
时间: 2024-06-05 09:03:36 浏览: 15
#pragma once是一种预处理指令,用于保证头文件只被编译一次。当多个文件中都包含同一个头文件时,#pragma once能够避免重复编译,提高编译效率。
与之类似的还有另一种预处理指令 #ifndef,#define和#endif,它们也可以保证头文件只被编译一次。但是 #pragma once更简洁,且不需要定义宏变量,所以在实际开发中更为常用。
需要注意的是,#pragma once指令在不同的编译器上可能存在差异,不保证在所有平台上都能正常工作。
相关问题
#pragma once #include<QGraphicsItem> #include<QGraphicsRectItem> #include<QGraphicsSceneMouseEvent> class GraphicsItem :public QGraphicsRectItem { public: GraphicsItem(QGraphicsItem* parent = nullptr); private: QPointF m_centerPointF; bool m_bResizing; };更改这个类,使创建出来的Item可以拉伸大小和移动位置
要使创建的GraphicsItem可以拉伸大小和移动位置,您可以对GraphicsItem类进行以下更改:
```cpp
#pragma once
#include <QGraphicsItem>
#include <QGraphicsRectItem>
#include <QGraphicsSceneMouseEvent>
class GraphicsItem : public QGraphicsRectItem
{
public:
GraphicsItem(QGraphicsItem* parent = nullptr);
protected:
void mousePressEvent(QGraphicsSceneMouseEvent* event) override;
void mouseMoveEvent(QGraphicsSceneMouseEvent* event) override;
void mouseReleaseEvent(QGraphicsSceneMouseEvent* event) override;
private:
QPointF m_pressPos;
QRectF m_originalRect;
bool m_bResizing;
};
```
在这个更改过的类中,我们添加了三个鼠标事件处理函数:mousePressEvent,mouseMoveEvent和mouseReleaseEvent。这些函数将帮助我们实现拉伸和移动功能。
```cpp
#include "GraphicsItem.h"
GraphicsItem::GraphicsItem(QGraphicsItem* parent)
: QGraphicsRectItem(parent)
{
setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
m_bResizing = false;
}
void GraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
m_pressPos = event->pos();
m_originalRect = rect();
if (m_originalRect.contains(m_pressPos))
{
m_bResizing = true;
setCursor(Qt::SizeAllCursor);
}
else
{
m_bResizing = false;
setCursor(Qt::ClosedHandCursor);
}
QGraphicsRectItem::mousePressEvent(event);
}
void GraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
{
if (m_bResizing)
{
QPointF delta = event->pos() - m_pressPos;
QRectF newRect = m_originalRect.adjusted(0, 0, delta.x(), delta.y());
setRect(newRect);
}
else
{
QGraphicsRectItem::mouseMoveEvent(event);
}
}
void GraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
{
m_bResizing = false;
setCursor(Qt::ArrowCursor);
QGraphicsRectItem::mouseReleaseEvent(event);
}
```
在这些鼠标事件处理函数中,我们根据鼠标操作来更新矩形的大小和位置。如果按下鼠标时位于矩形内部,我们将启用拉伸功能,并将光标设置为SizeAllCursor,表示可以拉伸。如果按下鼠标时位于矩形外部,我们将启用移动功能,并将光标设置为ClosedHandCursor,表示可以移动。在拖动鼠标时,我们根据鼠标的偏移量来调整矩形的大小或位置。
现在,您可以使用更改后的GraphicsItem类来创建可拉伸大小和可移动位置的Item了。
qt中调用以下Interface_CppDllMingW.h定义的C++动态dll库,在qi中直接引入以下代码的Interface_CppDllMingW.h提示语法错误,给出在qt中引入的Interface_C'p'p'DllMingW.h的代码;Interface_CppDllMingW.h的代码如下:#pragma once #ifdef INTERFACE_CPPDLLMINGW_EXPORTS #define INTERFACE_CPPDLLMINGW_API extern "C" __declspec(dllexport) #else #define INTERFACE_CPPDLLMINGW_API extern "C" __declspec(dllimport) #endif public ref class Interface_CppDllMingW { public: // Interface_CppDllMingW() = default; // ~Interface_CppDllMingW() = default; virtual int mingw_add(int a, int b) = 0; virtual void mingw_substract(int a, int b, int* c) = 0; virtual void mingw_showBox(const char* str) = 0; virtual short win32_Sync_isUSBExist() =0; virtual short win32_Sync_READ_status(const char* MainProg, const char* CurProg, int* CurSeq, const char* Mode, const char* Status, const char* Alarm, const char* EMG) = 0; }; INTERFACE_CPPDLLMINGW_API Interface_CppDllMingW^ CALL_API();
在Qt中调用Interface_CppDllMingW.h定义的C动态dll库需要注意以下几点:
1. 在Qt项目中引入dll库需要在.pro文件中添加LIBS += -L库路径 -l库名称
2. 如果dll库中的函数使用了STL容器,则需要在Qt项目中加入CONFIG += c++11
3. 在引入Interface_CppDllMingW.h文件时,需要将文件名中的p字母改为大写P字母,即Interface_CPPDllMingW.h
下面是在Qt中引入Interface_CPPDllMingW.h的代码示例:
```
// 引入头文件
#include "Interface_CPPDllMingW.h"
// 声明dll库中的函数指针
typedef int(*DLLFunc)(int, const char*);
DLLFunc pFunc = nullptr;
// 加载dll库
QLibrary myLib("myDll.dll");
// 判断库是否加载成功
if(myLib.load())
{
// 获取库中的函数指针
pFunc = (DLLFunc)myLib.resolve("myDllFunc");
}
// 使用库中的函数指针
if(pFunc)
{
int result = pFunc(1, "hello");
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)