error: no matching function for call to ‘QFrame::QFrame(MainWindow&)’
-
wrote on 12 May 2021, 06:58 last edited by
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 :)
-
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 :)
@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...
-
@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.
-
wrote on 12 May 2021, 07:13 last edited by
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());
-
@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
-
@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
wrote on 12 May 2021, 07:35 last edited by@Christian-Ehrlicher Oh thanks, I thought it would behave like in PyQt but I was wrong, thanks again.
-
No, python and c++ are two completely different languages and concepts.
1/7