简体   繁体   中英

Create a window in qt with shape from an image

有人可以解释一下我如何根据图像中某个对象的形状在qt中创建一个窗口,例如我有一个树的图像,用它我需要创建一个树形的窗口。

After a long search , myself found a good solution , check out this ..

#include <QtGui>
 class  myMainWindow:public QMainWindow
 {
 public:
     myMainWindow():QMainWindow()
     {
    setMask((new QPixmap("saturn.png"))->mask());


    QPalette* palette = new QPalette();
    palette->setBrush(QPalette::Background,QBrush(QPixmap("saturn.png")));
    setPalette(*palette);   

    setWindowFlags(Qt::FramelessWindowHint);     
    QWidget *centralWidget = new QWidget(this);
    QGridLayout *layout = new QGridLayout();

    centralWidget->setLayout(layout);

    QPushButton* button1 = new QPushButton("Button 1");
    button1->setFixedSize(80,50);

    layout->addWidget(button1,0,0); 

    setCentralWidget(centralWidget);

     };
     ~myMainWindow(){};
 };

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    myMainWindow *window = new myMainWindow();    

    window->resize(600, 316);          
    window->show();
    return app.exec();
}

Here is a recipe for making a widget with a semi-transparent background colour. Just expand from there by making the background fully transparent, then display the tree image on top of that as a background image. Note that the widget will still behave like a rectangular widget in regards to laying out its child elements, so you probably need to deal with this using some custom layout inside the tree shape.

Start from the docs for QWidget::setMask . It has a version which takes a QBitmap and one that takes a QRegion. This is the fundamental function in getting a transparent widget. The toolkit also includes a clock example using the QRegion version -- I suspect a bitmap is just as easy though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM