// Used to as Root item
class CRoot: public QGraphicsObject
{
public:
CRoot(QGraphicsObject *opParent = NULL) : QGraphicsObject(opParent)
{}
...
};
// Leaf Item
class Chip : public QGraphicsObject
{
...
{
Q_UNUSED(opOPtion);
Q_UNUSED(opWidget);
opPainter->save();
double d_value = mdWidth * transform().m11();
if (d_value >= 7)
{
opPainter->drawShape(moShape);
}
else
{
opPainter->drawPoint(0, 0);
}
opPainter->restore();
}
private:
};
class CLayout : public QGraphicsObject
{
Q_OBJECT
public:
CLayout(QGraphicsObject *opParent = NULL) : QGraphicsObject(opParent)
{
}
CRoot *mopGetRoot()
{
return mopRoot;
}
signals:
void msigLoadDone();
public slots:
void mslotLoadItems()
{
mopRoot = new CRoot;
const int I_COUNT = 500000;
for (int i_cnt = 0; i_cnt < I_COUNT; ++i_cnt)
{
Chip *op_chip = new chip(CRoot);
op_chip->setPos(i_cnt / 10000, i_cnt / 10000);
mlstopItems.push_back(op_chip);
}
// move items from curThread to GUI thread.
foreach(Chip *op_item, mlstopItems)
{
}
emit msigLoadDone();
}
private:
CRoot *mopRoot;
QList<Chip *> mlstopItems;
};
{
Q_OBJECT
public:
...
void mvInitScene()
{
}
signals:
void msigGoToLoad();
public slots:
void mslotSetRootItem()
{
std::call_once(meFlag, &CMainWindow::mvInitScene, this);
mopScene()->addItem(mopLayout->mopGetRoot());
}
void mslotBtnClicked()
{
mopLayout = new Layout();
#ifdef USED_THREAD
connect(this, SIGNAL(msigGoToLoad()), mopLayout, SLOT(mslotLoadItems()));
connect(mopLayout, SIGNAL(msigLoadDone()), this, SLOT(mslotSetRootItem()));
connect(mopThread, SIGNAL(finished()), mopThread, SLOT(deleteLater()));
mopLayout->moveToThread(mopThread);
mopThread->start();
#else
mopLayout->mslotLOadItems();
mslotSetRootItem();
#endif
emit msigGoToLoad();
}
private:
CLayout *mopLayout;
std::once_flag meFlag;
};
// Used to as Root item
class CRoot: public QGraphicsObject
{
public:
CRoot(QGraphicsObject *opParent = NULL) : QGraphicsObject(opParent)
{}
QRectF boundingRect() { return QRectF(); }
...
};
// Leaf Item
class Chip : public QGraphicsObject
{
...
void paint(QPainter *opPainter, const QStyleOptionGraphicsItem *opOPtion, QWidget *opWidget)
{
Q_UNUSED(opOPtion);
Q_UNUSED(opWidget);
opPainter->save();
double d_value = mdWidth * transform().m11();
if (d_value >= 7)
{
opPainter->drawShape(moShape);
}
else
{
opPainter->drawPoint(0, 0);
}
opPainter->restore();
}
private:
QPainterPath moShape; // used to store the path of Chip
QGraphicsTextItem *mopLabel; // Label item of this cell.
};
class CLayout : public QGraphicsObject
{
Q_OBJECT
public:
CLayout(QGraphicsObject *opParent = NULL) : QGraphicsObject(opParent)
{
}
CRoot *mopGetRoot()
{
QReadLocker o_locker(&moLock);
return mopRoot;
}
signals:
void msigLoadDone();
public slots:
void mslotLoadItems()
{
QWriteLocker o_locker(&moLock);
mopRoot = new CRoot;
const int I_COUNT = 500000;
for (int i_cnt = 0; i_cnt < I_COUNT; ++i_cnt)
{
Chip *op_chip = new chip(CRoot);
op_chip->setPos(i_cnt / 10000, i_cnt / 10000);
mlstopItems.push_back(op_chip);
}
// move items from curThread to GUI thread.
moveToThread(QApplication::instance()->thread());
mopRoot->moveToThread(QApplication::instance()->thread());
foreach(Chip *op_item, mlstopItems)
{
op_item->moveToThread(QApplication::instance()->thread());
}
emit msigLoadDone();
}
private:
CRoot *mopRoot;
QList<Chip *> mlstopItems;
QReadWriteLock moLock;
};
class CMainWindow: public QMainWindow
{
Q_OBJECT
public:
...
void mvInitScene()
{
mopScene = new QGraphicsScene();
}
signals:
void msigGoToLoad();
public slots:
void mslotSetRootItem()
{
std::call_once(meFlag, &CMainWindow::mvInitScene, this);
mopScene()->addItem(mopLayout->mopGetRoot());
}
void mslotBtnClicked()
{
mopThread = new QThread();
mopLayout = new Layout();
#ifdef USED_THREAD
connect(this, SIGNAL(msigGoToLoad()), mopLayout, SLOT(mslotLoadItems()));
connect(mopLayout, SIGNAL(msigLoadDone()), this, SLOT(mslotSetRootItem()));
connect(mopThread, SIGNAL(finished()), mopThread, SLOT(deleteLater()));
mopLayout->moveToThread(mopThread);
mopThread->start();
#else
mopLayout->mslotLOadItems();
mslotSetRootItem();
#endif
emit msigGoToLoad();
}
private:
QThread *mopThread;
CLayout *mopLayout;
QGraphicsScene *mopScene;
std::once_flag meFlag;
};
To copy to clipboard, switch view to plain text mode
Bookmarks