我正在學習Qt,遇到了一個我找不出來的問題,所以我想問專家!Qt - 有沒有辦法在用戶界面上查看QImage「live」?
我正在一個應用程序,我想有一個QImage對象(使用格式QImage :: Format_RGB888),並能夠使用setPixel()方法操縱個別像素&保存圖像與QImageWriter .. 。 到現在爲止還挺好。這一切都有效。
我的這種顯示Qimage的方式是QMainWIndow包含一個QGraphicsView對象,並且我創建了一個QGraphicsScene,並在我的MainWindow graphicsView上設置了這個場景。
問題是,我希望能夠在用戶界面上顯示這個QImage,以便用戶可以在操作時看到像素的變化。 目前,我必須從圖像中重新生成一個QGraphicsPixmapItem,並且每次我想要看到新的更改時,將addPixmap()重新添加到場景中。
有沒有辦法直接查看QImage,以便立即看到所做的更改?我是否使用錯誤的對象來保存和/或顯示我的圖像?
我有一個簡單的例子(只是mainwindow.cpp部分...其他文件只是默認的東西)。該用戶界面只有一個按鈕(用於觸發QImage更改),並放置在屏幕上顯示QImage。
我搜索了互聯網,但還沒有遇到任何似乎相關的帖子。 如果有人有任何建議,我會很高興聽到他們! 感謝,
-Eric
QGraphicsScene *scene = NULL;
QGraphicsItem *line = NULL;
QImage *image = NULL;
QGraphicsPixmapItem *item = NULL;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene = new QGraphicsScene();
image = new QImage(60, 60, QImage::Format_RGB888);
image->fill(Qt::cyan);
ui->retranslateUi(this);
ui->graphicsView->setScene(scene);
ui->graphicsView->show();
line = (QGraphicsItem*) scene->addLine(QLine(20, 40, 300, 100),
QPen(Qt::red, 6, Qt::DashLine, Qt::FlatCap));
scene->setBackgroundBrush(QBrush(Qt::green, Qt::SolidPattern));
scene->addEllipse(40, 80, 300, 240,
QPen(Qt::blue, 10, Qt::DashDotDotLine, Qt::RoundCap));
item = new QGraphicsPixmapItem(QPixmap::fromImage(*image));
scene->addPixmap(item->pixmap());
// Connect the pushbutton to the buttonPressed method, below.
connect( ui->pushButton, SIGNAL(pressed()),
this, SLOT(buttonPressed()));
}
// Slot connected to the button being pressed.
// Manipulate some pixels, and show the results.
void MainWindow::buttonPressed()
{
printf("Now in buttonPressed...\n");
int x, y;
int offset = qrand();
QRgb px;
px = qRgb(20+offset, 10-offset, 30+offset);
for (x=0; x< 60; x++)
for(y=0; y< 60; y++)
{
image->setPixel(x, y, px);
}
// I'd like to NOT have to re-convert the image every time.
item = new QGraphicsPixmapItem(QPixmap::fromImage(*image));
scene->addPixmap(item->pixmap());
}
嗨馬丁,謝謝你看這個。我仍在學習Qt,但我會嘗試這些建議。我需要訪問各個像素,並且一個QPixmap沒有訪問函數允許這個(除非我失去了一些東西) – user1524761 2012-07-17 00:00:27