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. [solved]How to add a widget to window, when it is generated by method
Forum Updated to NodeBB v4.3 + New Features

[solved]How to add a widget to window, when it is generated by method

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 3.6k Views 3 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.
  • L Offline
    L Offline
    Lord of Noob
    wrote on last edited by Lord of Noob
    #1

    Hi there,

    I want in my app to generate some default Wifdget arrangements by a method.
    For example:

    //adds a label and a QTextEdit field to my window.
    createItem(Name, InputType::Text);
    

    My problem is that this is not working. If I try to return the objects from the Method, I run into an error which I do not understand as noob.

    /usr/include/qt/QtWidgets/qlabel.h:142: Fehler: 'QLabel::QLabel(const QLabel&)' is private Q_DISABLE_COPY(QLabel)

    If I define the naked pointers in the class as private variables the application Crashes.
    Mainwindow.h

    class MainWindow : public QMainWindow
    {
       Q_OBJECT
    
    public:
       MainWindow(QWidget *parent, std::string appname);
       ~MainWindow();
       void createCharItem(QString name, QString txtContent);
     private:
      //variables
      QVBoxLayout *section; //Stacks all items below each other
      QLabel *title;  // one Item to add
      //functions
      void createTitle(QString name);
    

    }

    implementation of void createTitle(QString name);

    void MainWindow::createTitle(QString name)
    {
         QLabel *title = new QLabel;
         title->setText(name);
         title->setLineWidth(40);
         int height = title->height();
         title->resize(this->width(),height);
         section->addWidget(title);
    }
    

    What do I miss?
    Or how to do this?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      can you show the implementation of createCharItem()?
      QObject (or inherited classes) instances cannot be created on the stack (you have to use pointers)

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      L 1 Reply Last reply
      0
      • M mcosta

        Hi and welcome to devnet,

        can you show the implementation of createCharItem()?
        QObject (or inherited classes) instances cannot be created on the stack (you have to use pointers)

        L Offline
        L Offline
        Lord of Noob
        wrote on last edited by Lord of Noob
        #3

        @mcosta
        Thanks the Welcome,

        Ohh, I am sorry i was tired. The

         createTitle(); 
        

        is a reduced version of

         createItem();
        

        I do not have the createItem yet. It will look similar only it will consinst of a 1xQHBoxLayout,1xQLabel and 1xQTestEdit or 1xQSpinBox.

        My Constructer of QMainWindow looks like:

        MainWindow::MainWindow(QWidget *parent,std::string appname) :QMainWindow(parent)
        {
           QTabWidget *charsheet = new QTabWidget;
           this->setCentralWidget(charsheet);
           QWidget *titlepage = new QWidget(this);
           QWidget *combatpage = new QWidget;
           charsheet->addTab(titlepage,"Heerosheeet");
           charsheet->addTab(combatpage,"combatsheet");
           QVBoxLayout *section = new QVBoxLayout(titlepage);
           createTitle(QString::fromStdString(appname));
        /* If I do it this way it works ...
            QLabel *title = new QLabel;
            title->setText(QString::fromStdString(appname));
            title->setLineWidth(40);
            int height = title->height();
            title->resize(this->width(),height);
           section->addWidget(title);
           */
        }
        

        Ohh I just have seen if I move the

         section->addWidget(title); 
        

        to main, it works. But why I can not use the add Widget Method in my own method?

        Edit:

        Ok, fixed one Bug: QLabel was wrong. But the program is still crashing this way.

        void MainWindow::createTitle(QString name)
        {
             title = new QLabel;
             title->setText(name);
             title->setLineWidth(40);
             int height = title->height();
             title->resize(this->width(),height);
             section->addWidget(title);
        }
        
        1 Reply Last reply
        0
        • L Offline
          L Offline
          Lord of Noob
          wrote on last edited by
          #4

          Okay, I now found 2 Exceptions:
          QXcbWindow: Unhandled client message: "_E_COMP_FLUSH"
          QXcbWindow: Unhandled client message: "_E_COMP_DUMP"

          I think I can catch them, but I do not know what to do with these Errors...

          Google is not makeing any sense.
          Marking the Handler and press F1 is not doing something :-D

          Any hints where to look?

          1 Reply Last reply
          0
          • A Offline
            A Offline
            alex_malyu
            wrote on last edited by
            #5

            My problem is that this is not working. If I try to return the objects from the Method, I run into an error which I do not understand as noob.
            /usr/include/qt/QtWidgets/qlabel.h:142: Fehler: 'QLabel::QLabel(const QLabel&)' is private Q_DISABLE_COPY(QLabel)

            There is an attempt to copy QObject derived object, probably cause you return object not the pointer.

            If You need to create object of QWidget (QObject) derived class in a function, function should look like:

            QWidget* createWidget();

            not a
            QWidget createWidget (), cause in such case object will have to be copied and it is forbidden.

            1 Reply Last reply
            0
            • L Offline
              L Offline
              Lord of Noob
              wrote on last edited by
              #6

              Cool, this works.
              I return now a Pointer, and add it in one line to the screen.

              I mark this solved. Even I still have no Idea why the one way does not work.

              great thanks.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                Because QObject is not a copyable class. See here for more information

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                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