Search code examples
c++qt

Way of creating my own Qt components/widgets/tools


I'm new to Qt and I'm looking for a way to create my own components. I need to create my own custom styled (needs to look specifically) component. The problem is I'm looking at the tutorials and I'm a bit baffled as to how to do this, because the tutorials show something else than I need to do.

I need to create something like my own plotwidget, containing two graphs (one of them editable by clicking and dragging control points by mouse) that will have accessible properties and slots/events like "plotchanged".

Now, what I'looking for is not someone to write me the code (would be nice though :D ) but someone to tell me where to start looking/ tell me what to do?

Like You need to

  1. create your own "custom widget"/"qml component"/"something else you don't know about cause you're new in Qt and don't know the names or where to look".

  2. You should also read something about this,this and maybe that little thing that is useful. And here, this is what you should be looking at.

I started a lot of QML tutorial since i started looking, but when i start them I'm not sure that they lead to what I'm ultimately looking for.

What I want to create is a "Plot widget" that shows two graphs, the thing that looks close to it might be "curves tool" like in photoshop or GIMP.


Solution

  • This is a little hard to answer as there are many things you need to do something like in your linked picture. So I will just address the two main things:

    Creating your own custom widget:

    Just create a class inherited from QWidget and add a Layout to it:

    class CustomWidget : public QWidget
    {
    public:
        CustomWidget ( QObject * parent ) : QWidget ( parent )
        {
            QVBoxLayout * lay = new QVBoxLayout;
    
            lay->addWidget ( new QTextEdit );
            lay->addWidget ( new QPushButton ( "Button" ) );
    
            this->setLayout ( lay );
        }
    };
    

    You can use this class anywhere in for example other QWidget based classes via layouts, just like I used new QLineEdit, you can use new CustomWidget in another class. There are also many other Layouts.

    Drawing graphs:

    QPainter is the class you run all your drawing with (lines, curves, etc). You can draw in a QWidget (or to be exact: a class inherited from QPaintDevice). To draw on a QWidget when appropriate, override paintEvent(QPaintEvent*).