我需要在qt-project周围创建C包装器,它将用作库。它使用QPainter的简单功能。
我创建了一个Qtx类:
// -- Qtx.h --
#ifndef QTX_H
#define QTX_H
#include <QWidget>
#include <QPainter>
class Qtx
{
private:
QPainter *painter;
QPixmap *pix;
public:
Qtx();
QPixmap* getPixmap();
void setPainterColor(int r, int g, int b);
void drawLine(int x1, int y1, int x2, int y2);
void drawCircle(int x, int y, int radius);
void drawEllipse(int x1, int y1, int x2, int y2);
void drawRectangle(int x1, int y1, int x2, int y2);
};
#endif // QTX_H
// -- Qtx.cpp --
#include "qtx.h"
Qtx::Qtx()
{
pix = new QPixmap(400,280);
QPalette palette;
pix->fill(palette.color(QPalette::Window));
painter = new QPainter(pix);
}
QPixmap* Qtx :: getPixmap()
{
return pix;
}
void Qtx::setPainterColor(int r, int g, int b)
{
QPen *pen = new QPen(QColor(r, g, b));
painter->setPen(*pen);
}
void Qtx :: drawLine(int x1, int y1, int x2, int y2)
{
painter->drawLine(x1,y1,x2,y2);
}
void Qtx :: drawCircle(int x, int y, int radius)
{
QPoint center(x, y);
painter->drawEllipse(center, radius, radius);
}
void Qtx :: drawEllipse(int x1, int y1, int x2, int y2)
{
QRect rect(x1, y1, x2, y2);
painter->drawEllipse(rect);
}
void Qtx :: drawRectangle(int x1, int y1, int x2, int y2)
{
QRect rect(x1, y1, x2, y2);
painter->drawRect(rect);
}
为这个类创建了C-wrapper:
/*qtx-c-wrapper.h*/
struct HQTx;
typedef struct HQTx HQTx;
HQTx * create();
void destroy(HQTx * h);
void setPainterColor(HQTx* v, int r, int g, int b);
void drawLine(HQTx* v, int x1, int y1, int x2, int y2);
void drawCircle(HQTx* v, int x, int y, int radius);
void drawEllipse(HQTx* v, int x1, int y1, int x2, int y2);
void drawRectangle(HQTx* v, int x1, int y1, int x2, int y2);
但是Qtx的成员只能在MainWindow中使用(据我所知),例如:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qtx.h"
#include <QGridLayout>
#include <QPushButton>
#include <QLabel>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
Qtx* tx = new Qtx();
QPixmap* p;
QLabel *label = new QLabel(this);
tx->setPainterColor(255,0,0);
tx->drawCircle(100,100,90);
p = tx->getPixmap();
ui->setupUi(this);
if(p != NULL)
{
label->setGeometry(0,0,400,280);
label->setPixmap(*p);
ui->centralWidget = label;
}
}
是否可以为MainWindow创建C-wrapper?我该怎么办? 我正在尝试创建一个库,在C ++项目中使用它大致如下(只是示例):
#include "QtxLib.h"
int main()
{
QtxCreateWindow w = QtxCreateWindow (800, 600);
QtxDrawLine (w, 320, 290, 320, 220);
QtxCircle (w, 320, 190, 30);
return 0;
}
由于