2016-04-27 162 views
1

我有這樣的對話窗口類的Qt:如何在Qt中的特定座標上顯示圖像?

class Board : public QDialog 
{ 
    Q_OBJECT 

public: 
    explicit Board(QWidget *parent = 0); 
    ~Board(); 

private: 
    Ui::Board *ui; 

    void mousePressEvent(QMouseEvent *mouseEvent); 
}; 

我想dislay用戶給定座標PNG圖像的功能mousePressEvent,這被稱爲每當用戶點擊對話窗口上的某個地方。所以我需要像displayImage("path/to/image.png", coordX, coordY);。我該怎麼做?

新代碼:

class Board : public QDialog 
{ 
public: 
    Board(QWidget *parent = 0) : 
    QDialog(parent), 
    ui(new Ui::Board), 
    view(&scene) 
{ 
    // Set background image 
    /**************/ 
    ui->setupUi(this); 

    QPixmap pix("path/background.png"); 
    ui->label_board->setPixmap(pix); 

    /**************/ 
    /Set layout for displaying other images on the background 
    QVBoxLayout *layout = new QVBoxLayout(this); 
    layout->addWidget(&view); 
    //or set the layout and the view in the designer if using Qt Creator 
} 

protected: 
virtual void mousePressEvent(QMouseEvent *mouseEvent) override 
{ 
    QGraphicsPixmapItem *item = new QGraphicsPixmapItem(QPixmap("path/to/image.png")); 
    scene.addItem(item); 
    item->setPos(coordX, coordY); 
} 

private: 
    Ui::Board *ui; 
    QGraphicsScene scene; 
    QGraphicsView view; 
}; 

label_board是500×500的標籤設置爲使用Qt Designer一些位置。

enter image description here

+0

這並不完全清楚你想要做什麼,你向我們展示的代碼什麼都不做。 –

+0

對不起,我以爲我很清楚。我試圖更好地解釋。是的,代碼什麼都不做,因爲我問你如何做我不能做的事情,所以我怎麼能把它寫到我的問題上呢? –

+0

我認爲你的編輯實際上做得很好。 –

回答

0

您將需要使用QGraphicsViewdocs)和QGraphicsScenedocs):

class Board : public QDialog 
{ 
public: 
    Board(QWidget *parent = 0) : 
     QDialog(parent), 
     ui(new Ui::Board), 
     view(&scene) 
    { 
     QVBoxLayout *layout = new QVBoxLayout(this); 
     layout->addWidget(&view); 
     //or set the layout and the view in the designer if using Qt Creator 

     //EDIT: add background like this first 
     QGraphicsPixmapItem *background = QGraphicsPixmapItem(QPixmap("path/to/background.png")); 
     scene.addItem(background); 
     background.setPos(0, 0); //position it to cover all the scene so at 0,0 which is the origin point 
     background.setScale(2.0); //scale the image to the scene rectangle to fill it 
     background.setZValue(-0.1); //to ensure it is always at the back 
    } 

protected: 
    virtual void mousePressEvent(QMouseEvent *mouseEvent) override 
    { 
     QGraphicsPixmapItem *item = new QGraphicsPixmapItem(QPixmap("path/to/image.png")); 
     scene.addItem(item); 
     item->setPos(coordX, coordY); 
    } 

private: 
    Ui::Board *ui; 
    QGraphicsScene scene; 
    QGraphicsView view; 
}; 

這是相當多了。另請注意,該位置是該項目左上角的位置(圖像)。如果你想要居中,你需要根據物品的比例(圖像)來調整位置。

+0

非常感謝!現在的問題是,我已經在對話框窗口中顯示了一個圖像標籤,我希望新圖像(您告訴我如何顯示該圖像)顯示在所提及標籤的頂部,以便標籤將是一個靜態背景,我想添加和刪除其他圖像,因爲我需要。我將它添加到上面的代碼中。 –

+0

@ T.Syk你可以用同樣的方式設置QGraphicsScene的背景圖像。我強烈建議使用QGraphicsScene來進行所有的圖像渲染。 QLabel的方法是可能的,但意圖是爲了不同的目的(不是作爲背景進行進一步的工作,就像一塊板子一樣,所以你可以簡單地將背景自己添加到場景中,就像我先顯示的那樣) – Resurrection

+0

@ T.Syk I have編輯我的答案以覆蓋背景圖像以及對話框的構造函數,還可以在場景中設置背景畫筆()。此外,您可能希望將背景的z pozition(setZValue)調整爲-0.1之類的內容以確保背景將會總是在背景中,而不是遮住你以後添加的任何東西 – Resurrection

相關問題