pro:
QT += opengl
TARGET = listclass
TEMPLATE = app
SOURCES += main.cpp
main.cpp:
#include <QApplication>
#include <QDebug>
#include <QtGui>
#include <QtCore>
#include <QtOpenGL>
{ Q_OBJECT
public:
MainForm() {
connect(loadimg_pb,SIGNAL(clicked()),this,SLOT(LoadImages()));
connect(testimg_pb,SIGNAL(clicked()),this,SLOT(TestImage()));
hl->addWidget(loadimg_pb,0);
hl->addWidget(testimg_pb,0);
hl->addStretch(1);
vl->addWidget(view);
vl->addLayout(hl);
setLayout(vl);
}
protected slots:
void LoadImages() {
QStringList filenames
= QFileDialog::getOpenFileNames(0,
"Open Image",
"",
"Images (*.jpg;*.jpeg;*.bmp;*.png)");
for (int i=0;i<filenames.length();++i)
{
if (!img.isNull()) {
pi->setPos(qrand()*500/RAND_MAX,qrand()*500/RAND_MAX);
}
}
}
void TestImage() {
// Setup the color palette for indexed colormode
QRgb value;
value = qRgb(122, 163, 39); // 0xff7aa327
image.setColor(0, value);
value = qRgb(237, 187, 51); // 0xffedba31
image.setColor(1, value);
value = qRgb(189, 149, 39); // 0xffbd9527
image.setColor(2, value);
// Set pixels
for (int x=0;x<300;++x) {
for (int y=0;y<150;++y) {
image.setPixel(x, y, 0);
}
}
for (int x=0;x<150;++x) {
for (int y=150;y<300;++y) {
image.setPixel(x, y, 2);
}
}
for (int x=150;x<300;++x) {
for (int y=150;y<300;++y) {
image.setPixel(x, y, 1);
}
}
pi->setPos(qrand()*500/RAND_MAX,qrand()*500/RAND_MAX);
}
private:
};
int main(int argc, char *argv[])
{
app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
MainForm form;
form.setGeometry(100,100,800,600);
form.show();
return app.exec();
}
#include "main.moc"
pro:
QT += opengl
TARGET = listclass
TEMPLATE = app
SOURCES += main.cpp
main.cpp:
#include <QApplication>
#include <QDebug>
#include <QtGui>
#include <QtCore>
#include <QtOpenGL>
class MainForm : public QWidget
{ Q_OBJECT
public:
MainForm() {
scene = new QGraphicsScene();
view = new QGraphicsView(scene);
view->setViewport(new QGLWidget());
QPushButton* loadimg_pb = new QPushButton("Load Image(s) ..");
connect(loadimg_pb,SIGNAL(clicked()),this,SLOT(LoadImages()));
QPushButton* testimg_pb = new QPushButton("Test Image!");
connect(testimg_pb,SIGNAL(clicked()),this,SLOT(TestImage()));
QHBoxLayout* hl = new QHBoxLayout();
hl->addWidget(loadimg_pb,0);
hl->addWidget(testimg_pb,0);
hl->addStretch(1);
QVBoxLayout* vl = new QVBoxLayout();
vl->addWidget(view);
vl->addLayout(hl);
setLayout(vl);
}
protected slots:
void LoadImages() {
QStringList filenames = QFileDialog::getOpenFileNames(0, "Open Image", "", "Images (*.jpg;*.jpeg;*.bmp;*.png)");
for (int i=0;i<filenames.length();++i)
{
QImage img(filenames.at(i));
if (!img.isNull()) {
QGraphicsPixmapItem* pi = scene->addPixmap(QPixmap::fromImage(img).scaledToWidth(50));
pi->setFlag(QGraphicsItem::ItemIsMovable,true);
pi->setFlag(QGraphicsItem::ItemIsSelectable,true);
pi->setPos(qrand()*500/RAND_MAX,qrand()*500/RAND_MAX);
}
}
}
void TestImage() {
QImage image(300, 300, QImage::Format_Indexed8);
// Setup the color palette for indexed colormode
QRgb value;
value = qRgb(122, 163, 39); // 0xff7aa327
image.setColor(0, value);
value = qRgb(237, 187, 51); // 0xffedba31
image.setColor(1, value);
value = qRgb(189, 149, 39); // 0xffbd9527
image.setColor(2, value);
// Set pixels
for (int x=0;x<300;++x) {
for (int y=0;y<150;++y) {
image.setPixel(x, y, 0);
}
}
for (int x=0;x<150;++x) {
for (int y=150;y<300;++y) {
image.setPixel(x, y, 2);
}
}
for (int x=150;x<300;++x) {
for (int y=150;y<300;++y) {
image.setPixel(x, y, 1);
}
}
QGraphicsPixmapItem* pi = scene->addPixmap(QPixmap::fromImage(image));
pi->setFlag(QGraphicsItem::ItemIsMovable,true);
pi->setFlag(QGraphicsItem::ItemIsSelectable,true);
pi->setPos(qrand()*500/RAND_MAX,qrand()*500/RAND_MAX);
}
private:
QGraphicsScene* scene;
QGraphicsView* view;
QGLWidget* glwidget;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
MainForm form;
form.setGeometry(100,100,800,600);
form.show();
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks