Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Adding A Class In QtCreator
Forum Updated to NodeBB v4.3 + New Features

Adding A Class In QtCreator

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 5.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    ofmrew
    wrote on last edited by
    #1

    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?

    aha_1980A 1 Reply Last reply
    0
    • O ofmrew

      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?

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by aha_1980
      #2

      @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 has to stay free or it will die.

      1 Reply Last reply
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

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

        alt text

        1 Reply Last reply
        2
        • O Offline
          O Offline
          ofmrew
          wrote on last edited by
          #4

          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.

          mrjjM 1 Reply Last reply
          0
          • O ofmrew

            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.

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            @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

            O 1 Reply Last reply
            0
            • mrjjM mrjj

              @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

              O Offline
              O Offline
              ofmrew
              wrote on last edited by
              #6

              @mrjj Thanks. I will look into that.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved