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. error: no matching function for call to ‘QFrame::QFrame(MainWindow&)’

error: no matching function for call to ‘QFrame::QFrame(MainWindow&)’

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 646 Views
  • 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.
  • A Offline
    A Offline
    Anosema
    wrote on last edited by
    #1

    Hello,
    I'm new to C++ but I used to do a lot of PyQt.
    I need to subclass a QTreeWidgetItem which will own a QFrame which has a subclassed QMainWindow as parent. Here is the full code:

    main.cpp

    #include <QApplication>
    #include "window.h"
    #include <iostream>
    
    struct MainWindow;
    struct WritingTab;
    
    WritingTab::WritingTab(MainWindow *par) : QTreeWidgetItem()
    {
        writeFrame = new QFrame(*par);
        titleEntry = new QLineEdit(writeFrame);
        noteEntry = new QPlainTextEdit(writeFrame);
        clearButton = new QPushButton(writeFrame);
        writeFrame->setGeometry(100, 0, writeFrame->width() - 100, writeFrame->height());
    }
    
    MainWindow::MainWindow() : QMainWindow()
    {
        tabVector = {new WritingTab(this)};
        treeWidget = new QTreeWidget(this);
        treeWidget->addTopLevelItem(tabVector.at(0));
        treeWidget->setColumnCount(1);
        setStyleSheet("*{border: 1px solid red;}");
        setGeometry(10, 10, 500, 500);
        show();
    }
    void MainWindow::resizeEvent(QResizeEvent *event)
    {
        treeWidget->setGeometry(0, 0, 100, height());
        event->accept();
    };
    
    int main(int argc, char **argv)
    {
        QApplication app(argc, argv);
        MainWindow window;
        return app.exec();
    }
    

    and window.h

    #ifndef WINDOW_H
    #define WINDOW_H
    #include <QPlainTextEdit>
    #include <QPushButton>
    #include <QMainWindow>
    #include <QTreeWidget>
    #include <QTreeWidgetItem>
    #include <QLineEdit>
    #include <QFrame>
    #include <iostream>
    
    struct MainWindow;
    struct WritingTab;
    
    class WritingTab : public QTreeWidgetItem
    {
    public:
        explicit WritingTab(MainWindow *par);
        QFrame *writeFrame;
        QLineEdit *titleEntry;
        QPlainTextEdit *noteEntry;
        QPushButton *clearButton;
    
    private:
    signals:
    public slots:
    };
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    public:
        explicit MainWindow();
        QList<WritingTab *> tabVector;
        QTreeWidget *treeWidget;
        void resizeEvent(QResizeEvent *event);
    
    private:
    signals:
    public slots:
    };
    #endif
    

    But I have an error while compiling:

    main.cpp: In constructor ‘WritingTab::WritingTab(MainWindow*)’:
    main.cpp:10:33: error: no matching function for call to ‘QFrame::QFrame(MainWindow&)’
    writeFrame = new QFrame(*par);
    

    I don't really know how to fix it so any help is welcomed :)

    jsulmJ 1 Reply Last reply
    0
    • A Anosema

      Hello,
      I'm new to C++ but I used to do a lot of PyQt.
      I need to subclass a QTreeWidgetItem which will own a QFrame which has a subclassed QMainWindow as parent. Here is the full code:

      main.cpp

      #include <QApplication>
      #include "window.h"
      #include <iostream>
      
      struct MainWindow;
      struct WritingTab;
      
      WritingTab::WritingTab(MainWindow *par) : QTreeWidgetItem()
      {
          writeFrame = new QFrame(*par);
          titleEntry = new QLineEdit(writeFrame);
          noteEntry = new QPlainTextEdit(writeFrame);
          clearButton = new QPushButton(writeFrame);
          writeFrame->setGeometry(100, 0, writeFrame->width() - 100, writeFrame->height());
      }
      
      MainWindow::MainWindow() : QMainWindow()
      {
          tabVector = {new WritingTab(this)};
          treeWidget = new QTreeWidget(this);
          treeWidget->addTopLevelItem(tabVector.at(0));
          treeWidget->setColumnCount(1);
          setStyleSheet("*{border: 1px solid red;}");
          setGeometry(10, 10, 500, 500);
          show();
      }
      void MainWindow::resizeEvent(QResizeEvent *event)
      {
          treeWidget->setGeometry(0, 0, 100, height());
          event->accept();
      };
      
      int main(int argc, char **argv)
      {
          QApplication app(argc, argv);
          MainWindow window;
          return app.exec();
      }
      

      and window.h

      #ifndef WINDOW_H
      #define WINDOW_H
      #include <QPlainTextEdit>
      #include <QPushButton>
      #include <QMainWindow>
      #include <QTreeWidget>
      #include <QTreeWidgetItem>
      #include <QLineEdit>
      #include <QFrame>
      #include <iostream>
      
      struct MainWindow;
      struct WritingTab;
      
      class WritingTab : public QTreeWidgetItem
      {
      public:
          explicit WritingTab(MainWindow *par);
          QFrame *writeFrame;
          QLineEdit *titleEntry;
          QPlainTextEdit *noteEntry;
          QPushButton *clearButton;
      
      private:
      signals:
      public slots:
      };
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      public:
          explicit MainWindow();
          QList<WritingTab *> tabVector;
          QTreeWidget *treeWidget;
          void resizeEvent(QResizeEvent *event);
      
      private:
      signals:
      public slots:
      };
      #endif
      

      But I have an error while compiling:

      main.cpp: In constructor ‘WritingTab::WritingTab(MainWindow*)’:
      main.cpp:10:33: error: no matching function for call to ‘QFrame::QFrame(MainWindow&)’
      writeFrame = new QFrame(*par);
      

      I don't really know how to fix it so any help is welcomed :)

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Anosema said in error: no matching function for call to ‘QFrame::QFrame(MainWindow&)’:

      writeFrame = new QFrame(*par);

      Should be

      writeFrame = new QFrame(par);
      

      as it takes a pointer...

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @Anosema said in error: no matching function for call to ‘QFrame::QFrame(MainWindow&)’:

        I don't really know how to fix it so any help is welcomed :)

        QFrame needs a pointer as parent, no object.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Anosema
          wrote on last edited by
          #4

          Thanks I did remove the * from

          writeFrame = new QFrame(*par);
          

          and the frame now show. But, when I try this line, the compiler says the object returned by parent() is a QObject, not the MainWindow

          writeFrame->setGeometry(100, 0, writeFrame->parent()->width() - 100, writeFrame->parent()->height());
          
          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Anosema said in error: no matching function for call to ‘QFrame::QFrame(MainWindow&)’:

            says the object returned by parent() is a QObject, not the MainWindow

            And it's right...: https://doc.qt.io/qt-5/qobject.html#parent

            You maybe want https://doc.qt.io/qt-5/qwidget.html#parentWidget

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            A 1 Reply Last reply
            2
            • Christian EhrlicherC Christian Ehrlicher

              @Anosema said in error: no matching function for call to ‘QFrame::QFrame(MainWindow&)’:

              says the object returned by parent() is a QObject, not the MainWindow

              And it's right...: https://doc.qt.io/qt-5/qobject.html#parent

              You maybe want https://doc.qt.io/qt-5/qwidget.html#parentWidget

              A Offline
              A Offline
              Anosema
              wrote on last edited by
              #6

              @Christian-Ehrlicher Oh thanks, I thought it would behave like in PyQt but I was wrong, thanks again.

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                No, python and c++ are two completely different languages and concepts.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                1

                • Login

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