Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a window in qt with shape from an image

Can some one explain me how to make a window in qt according to the shape of some object in an image , for example i have an image of a tree , using that i need to create a window in the shape of a tree ..

like image 895
Akhil Thayyil Avatar asked Apr 20 '12 06:04

Akhil Thayyil


People also ask

What is QWidget?

The QWidget class is the base class of all user interface objects. The widget is the atom of the user interface: it receives mouse, keyboard and other events from the window system, and paints a representation of itself on the screen. Every widget is rectangular, and they are sorted in a Z-order.

What is QWidget * parent?

The tree-like organization of QWidgets (and in fact of any QObjects ) is part of the memory management strategy used within the Qt-Framework. Assigning a QObject to a parent means that ownership of the child object is transferred to the parent object. If the parent is deleted, all its children are also deleted.

How do I change the title of a window in Qt?

EDIT: If you are using QtDesigner, on the property tab, there is an editable property called windowTitle which can be found under the QWidget section. The property tab can usually be found on the lower right part of the designer window.

How do I change the size of a widget in Qt?

Custom Widgets in LayoutsReimplement QWidget::sizeHint() to return the preferred size of the widget. Reimplement QWidget::minimumSizeHint() to return the smallest size the widget can have. Call QWidget::setSizePolicy() to specify the space requirements of the widget.


2 Answers

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();
}
like image 84
Akhil Thayyil Avatar answered Nov 04 '22 09:11

Akhil Thayyil


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.

like image 2
teukkam Avatar answered Nov 04 '22 07:11

teukkam