Adding A Class In QtCreator



  • In QtCreator I add a class with QPlainTextEdit as the base class and I get the following;

    #ifndef MYTEXTEDITOR_H
    #define MYTEXTEDITOR_H

    class MyTextEditor : public QPlainTextEdit
    {
    public:
    MyTextEditor();
    };

    #endif // MYTEXTEDITOR_H

    #include "mytexteditor.h"

    MyTextEditor::MyTextEditor(){}

    However, I need the something like the following:

    #ifndef MYPLAINTEXTEDITOR_H
    #define MYPLAINTEXTEDITOR_H

    #include <QObject>
    #include <QPlainTextEdit>
    #include "mainwindow.h"

    class MyPlainTextEditor : public QPlainTextEdit
    {
    Q_OBJECT
    public:
    MyPlainTextEditor(QWidget *parent = 0);
    };

    #endif // MYPLAINTEXTEDITOR_H

    MyPlainTextEditor::MyPlainTextEditor(QWidget *parent) : QPlainTextEdit(parent)
    {

    }

    My objective to promote the PlainTextEdit widget to MyPlainTextEditor. In the past I have added a MyCanvas class with QWidget as the base class to promote a QWidget and have never had to modify the code generated by QtCreator. If the base class is something other than QWidget, will I need to modify the code?



  • @ofmrew said in Adding A Class In QtCreator:

    In the past I have added a MyCanvas class with QWidget as the base class to promote a QWidget and have never had to modify the code generated by QtCreator. If the base class is something other than QWidget, will I need to modify the code?

    Yes, I also already noticed that the wizard is not clever enough to add the needed includes. (Btw: #include <QPlainTextEdit> should be enought.) So you will have to edit this template a bit to get a compilable source file.

    If you are annoyed about that, please file a bug at bugreports.qt.io


  • Qt Champions 2016

    Hi
    Yes it only support the shown includes.
    In case of custom base class , no includes are added.

    alt text



  • So, if the base class is custom, i.e., not listed in the drop-down list, then it is up to the programmer to finish the prototype and the constructor.


  • Qt Champions 2016

    @ofmrew
    well its faster to use QWidget and just change to the wanted type.
    If you use custom base class, it seems it does not call base class in ctor init list.

    test::test(QWidget *parent) : QWidget(parent) <<< missing with custom

    If you wish, they are easy to change
    C:\Qt\Tools\QtCreator\share\qtcreator\templates\wizards\classes\cpp

    http://doc.qt.io/qtcreator/creator-project-wizards.html



  • @mrjj Thanks. I will look into that.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.