这个其实是一个线程安全的int mapper
void alThunkInit(void);
void alThunkExit(void);
ALuint alThunkAddEntry(ALvoid * ptr);
void alThunkRemoveEntry(ALuint index);
ALvoid *alThunkLookupEntry(ALuint index);
typedef struct {
ALvoid *ptr;
ALboolean InUse;
} ThunkEntry;
static ThunkEntry *g_ThunkArray;
static ALuint g_ThunkArraySize;
static CRITICAL_SECTION g_ThunkLock;
void alThunkInit(void)
{
InitializeCriticalSection(&g_ThunkLock);
g_ThunkArraySize = 1;
g_ThunkArray = calloc(1, g_ThunkArraySize * sizeof(ThunkEntry));
}
void alThunkExit(void)
{
free(g_ThunkArray);
g_ThunkArray = NULL;
g_ThunkArraySize = 0;
DeleteCriticalSection(&g_ThunkLock);
}
ALuint alThunkAddEntry(ALvoid *ptr)
{
ALuint index;
EnterCriticalSection(&g_ThunkLock);
for(index = 0;index < g_ThunkArraySize;index++)
{
if(g_ThunkArray[index].InUse == AL_FALSE)
break;
}
if(index == g_ThunkArraySize)
{
ThunkEntry *NewList;
NewList = realloc(g_ThunkArray, g_ThunkArraySize*2 * sizeof(ThunkEntry));
if(!NewList)
{
LeaveCriticalSection(&g_ThunkLock);
return 0;
}
memset(&NewList[g_ThunkArraySize], 0, g_ThunkArraySize*sizeof(ThunkEntry));
g_ThunkArraySize *= 2;
g_ThunkArray = NewList;
}
g_ThunkArray[index].ptr = ptr;
g_ThunkArray[index].InUse = AL_TRUE;
LeaveCriticalSection(&g_ThunkLock);
return index+1;
}
void alThunkRemoveEntry(ALuint index)
{
EnterCriticalSection(&g_ThunkLock);
if(index > 0 && index <= g_ThunkArraySize)
g_ThunkArray[index-1].InUse = AL_FALSE;
LeaveCriticalSection(&g_ThunkLock);
}
ALvoid *alThunkLookupEntry(ALuint index)
{
ALvoid *ptr = NULL;
EnterCriticalSection(&g_ThunkLock);
if(index > 0 && index <= g_ThunkArraySize)
ptr = g_ThunkArray[index-1].ptr;
LeaveCriticalSection(&g_ThunkLock);
return ptr;
}
用途比较广
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
void resizeEvent(QResizeEvent* event);
private:
QStateMachine* mMachine;
QSignalTransition* mTransition;
QState* mMaxState;
QState* mMinState;
QPushButton* mButton;
bool mResized;
};
Widget::Widget(QWidget *parent):
QWidget(parent)
{
mResized = false;
resize(640,480);
}
Widget::~Widget()
{
}
void Widget::resizeEvent(QResizeEvent* event)
{
if(size().height() == 0)
{
close();
return;
}
if(mResized)
return;
mButton = new QPushButton(this);
mButton->setText("Click");
mResized = true;
QPoint p = pos();
QSize s = size();
mMachine = new QStateMachine(this);
mMaxState = new QState(mMachine);
mMaxState->assignProperty(this,"geometry",QRect(p,s));
//connect(mMachine,SIGNAL(finished()),this,SLOT(callClose()));
mMinState = new QState(mMachine);
mMinState->assignProperty(this,"geometry",QRect(p.x(),p.y()+s.height()/2,s.width(),0));
//connect(mMinState,SIGNAL(entered()),this,SLOT(callClose()));
mMachine->setInitialState(mMaxState);
QPropertyAnimation* ani1 = new QPropertyAnimation(this,"geometry");
ani1->setDuration(300);
ani1->setEasingCurve(QEasingCurve::Linear);
QSignalTransition* transition = mMaxState->addTransition(mButton,SIGNAL(clicked()),mMinState);
transition->addAnimation(ani1);
mMachine->start();
}
不过有2个问题,当动画结束的时候,和这句相关connect(mMinState,SIGNAL(entered()),this,SLOT(callClose()));
可以退出程序
但是动画没执行 有空看下
setSizeConstraint(QLayout::SetNoConstraint);
QSignalMapper* mapper = new QSignalMapper(this);
for(int i=0;i<4;i++)
{
QRadioButton* button = new QRadioButton();
if(i == 0)
button->setChecked(true);
button->setText(items[i+1]);
buttonlayout->addWidget(button);
mapper->setMapping(button,i);
connect(button,SIGNAL(clicked()),mapper,SLOT(map()));
}
connect(mapper,SIGNAL(mapped(int)),this,SLOT(onClick(int)));
QFont font;
font.setPointSize(18);
painter.setFont(font);
QFontMetrics fm = painter.fontMetrics();
QString list[] = {"A","B","C","D","E"};
for(int i=0;i<5;i++)
{
int width = fm.width(list[i]);
int height = fm.ascent()+fm.descent();
QPair<int,int> pos(mDatas[i]);
QRectF rect(pos.first-width/2,pos.second-height/2,width,height);
painter.drawText(rect,list[i]);
}
class painter : public QWidget
{
Q_OBJECT
public:
painter(QWidget *parent = 0, Qt::WFlags flags = 0);
~painter();
private:
void paintEvent(QPaintEvent* e);
void clcPos(int x,int y,int r);
void renderCircle(QPainter& painter,int x,int y,int r);
void renderLine(int p1,int p2);
void mousePressEvent(QMouseEvent*);
void mouseReleaseEvent(QMouseEvent*);
int length(int x1,int y1,int x2,int y2);
int min(int x,int y,int& m);
private:
bool mMousePress;
QVector<QPair<int,int> > mPosList;
};
#endif
painter::painter(QWidget* parent,Qt::WFlags flags):
QWidget(parent,flags)
{
setGeometry(100,100,640,480);
setBackgroundRole(QPalette::Base);
setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
mMousePress = false;
}
painter::~painter()
{
}
void painter::paintEvent(QPaintEvent* e)
{
mPosList.clear();
int w = width();
int h = height();
float p = 3.14159265f;
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing,true);
for(int i=0;i<5;i++)
{
float delta = p*0.4*i - .5*p;
float cos = cosf(delta)*148;
float sin = sinf(delta)*148;
int x = w/2 + cos;
int y = h/2 + sin;
mPosList.push_back(QPair<int,int>(x,y));
}
renderLine(0,2);
renderLine(1,3);
renderLine(2,4);
renderLine(3,0);
renderLine(4,1);
for(int i=0;i<5;i++)
{
QPair<int,int> pos = mPosList[i];
renderCircle(painter,pos.first,pos.second,48);
}
}
void painter::clcPos(int x,int y,int r)
{
mPosList.push_back(QPair<int,int>(x,y));
}
void painter::renderCircle(QPainter& painter0,int x,int y,int r)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing,true);
painter.setPen(QPen(Qt::blue,2,Qt::SolidLine));
painter.setBrush(QBrush(Qt::green,Qt::SolidPattern));
painter.drawRoundRect(x-r/2,y-r/2,r,r,99,99);
}
void painter::mousePressEvent(QMouseEvent* e)
{
if(e->button() == Qt::LeftButton)
{
mMousePress = true;
int x = e->x();
int y = e->y();
const int off2 = 48*48;
int m = 9999;
int offset = min(x,y,m);
if(m < 36*36)
std::cout<<"here:"<<offset<<std::endl;
}
}
int painter::min(int x,int y,int& m)
{
int offset = 0;
for(int i=0;i<5;i++)
{
QPair<int,int> pos(mPosList[i]);
int len = length(x,y,pos.first,pos.second);
if(len < m)
{
m = len;
offset = i;
}
}
return offset;
}
void painter::mouseReleaseEvent(QMouseEvent* e)
{
mMousePress = false;
}
int painter::length(int x1,int y1,int x2,int y2)
{
return (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);
}
void painter::renderLine(int p1,int p2)
{
QPair<int,int> pos1 = mPosList[p1];
QPair<int,int> pos2 = mPosList[p2];
float k = pos2.second - pos1.second;
k /= (float)(pos2.first - pos1.first);
std::cout<<k<<std::endl;
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing,true);
painter.setPen(QPen(Qt::blue,2,Qt::DotLine));
painter.drawLine(pos1.first,pos1.second,pos2.first,pos2.second);
}
class scrollTest : public QWidget
{
Q_OBJECT
public:
scrollTest(QWidget *parent = 0, Qt::WFlags flags = 0);
~scrollTest();
private:
void createWidget();
QWidget* createGroupBox(const QString& title,int n);
};
scrollTest::scrollTest(QWidget* parent,Qt::WFlags flags):
QWidget(parent,flags)
{
createWidget();
}
scrollTest::~scrollTest()
{
}
void scrollTest::createWidget()
{
resize(300,360);
QGridLayout* gridLayout = new QGridLayout(this);
QScrollArea* scrollArea = new QScrollArea(this);
//scrollArea->setWidgetResizable(false);
//scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
QWidget* scrollAreaWidgetContents = new QWidget();
//scrollAreaWidgetContents->setGeometry(QRect(0, 0, 380, 251));
scrollArea->setWidget(scrollAreaWidgetContents);
gridLayout->addWidget(scrollArea, 0, 0, 1, 2);
QSpacerItem* horizontalSpacer = new QSpacerItem(298,20,QSizePolicy::Expanding,QSizePolicy::Minimum);
gridLayout->addItem(horizontalSpacer, 1, 0, 1, 1);
QPushButton* pushButton = new QPushButton(this);
pushButton->setText("OK");
gridLayout->addWidget(pushButton, 1, 1, 1, 1);
QWidget* content = new QWidget();
content->setMinimumHeight(280);
//content->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
QVBoxLayout* vlayout = new QVBoxLayout(content);
//scrollArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
vlayout->addWidget(createGroupBox("box1",3));
vlayout->addWidget(createGroupBox("box2",5));
vlayout->addStretch(1);
scrollArea->setWidget(content);
//scrollArea->setFixedHeight(420);
}
QWidget* scrollTest::createGroupBox(const QString& title,int n)
{
QGroupBox* box = new QGroupBox(title);
QVBoxLayout* boxlayout = new QVBoxLayout(box);
for(int i=0;i<n;i++)
boxlayout->addWidget(new QRadioButton(QString("%1").arg(i)));
return box;
}
网上看到的例子都有点问题
上一个我测试过的例子:
#include <qtest.h>
#include <QObject>
class shopTest : public QObject
{
Q_OBJECT
private slots:
void test();
private:
void test_discount();
void test_item();
void te函数st_coupon();
void test_date();
};
经过测试 slots簇只有第一个函数会被执行到。
void shopTest::test()
{
}
void shopTest::test_discount()
{
}
void shopTest::test_item()
{
}
void shopTest::test_coupon()
{
..
}
void shopTest::test_date()
{
..
}
QTEST_MAIN(shopTest)
解决方案:如果从文本载入的话,确定文本为utf8编码,然后调用
QString text = QString::fromUtf8(file.readAll());即可
一直想做个类似matlab的软件,支持矩阵运算,线代,微积分求解等功能
大家有啥好的建议没?
去年业余主要开发了
GAudio今年开发了
华佗中药大师业余版本,马上就要出入门版本咯
一直想靠写软件发财
基本用法如下:
foreach(agentiaItem* item,mTree)
{
QTreeWidgetItem* parent = new QTreeWidgetItem(QStringList(item->getItemName()));
tree->addTopLevelItem(parent);
QStringList list = item->getItemList();
foreach(QString item,list)
{
QTreeWidgetItem* child = new QTreeWidgetItem(QStringList(item));
parent->addChild(child);
}
}
不过构造QTreeWidgetItem的时候,不太理解为什么要使用QStringList而不是QString?