Invalid use of ‘this’ in non-member function
-
I keep getting ^^ error message. This is the code:
test.cpp:
@
QPushButton* saveButton = new QPushButton("Save");
QObject::connect(saveButton,SIGNAL(clicked()), this, SLOT(Save()));
QObject::connect(saveButton,SIGNAL(clicked()), summary, SLOT(clear()));
@test.hpp
@
#ifndef TEST_HPP
#define TEST_HPP#include <QWidget>
class QPushButton;
class QComboBox;
class QDateTimeEdit;
class QTextEdit;
class QLabel;class Press: public QWidget
{
Q_OBJECTpublic:
Press(QWidget* parent = 0);public slots:
void Save();
void Discard();private:
QLabel* startLabel;
QLabel* finishLabel;
QLabel* locationLabel;
QLabel* summaryLabel;
QPushButton* saveButton;
QPushButton* discardButton;
QComboBox * location;
QTextEdit * summary;
QDateTimeEdit* startDate;
QDateTimeEdit* finishDate;
};
#endif
@[edit: corrected use of coding tags SGaist]
-
Hi and welcome to devnet,
@
test.cpp:
QPushButton* saveButton = new QPushButton("Save");
QObject::connect(saveButton,SIGNAL(clicked()), this, SLOT(Save()));
QObject::connect(saveButton,SIGNAL(clicked()), summary, SLOT(clear()));
@This is wrong, you can't just throw code in the cpp file. You have to implement the constructors/functions of your class.
-
Qt's documentation is full of "examples and tutorials":http://qt-project.org/doc/qt-5/qtexamplesandtutorials.html
-
@
#include "test.hpp"
#include <QApplication>
#include <QLabel>
#include <QWidget>
#include <QDateTimeEdit>
#include <QComboBox>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QTextEdit>int main(int argc, char* argv[])
{
QApplication app(argc, argv);QDateTimeEdit* startDate= new QDateTimeEdit(); QDateTimeEdit* finishDate = new QDateTimeEdit(); QTextEdit * summary = new QTextEdit(); //QWidget* window = new QWidget; QLabel* startLabel = new QLabel("Start"); QLabel* finishLabel = new QLabel("Finish:"); QLabel* locationLabel = new QLabel("Location:"); QLabel* summaryLabel = new QLabel("Summary:"); QPushButton* saveButton = new QPushButton("Save"); QObject::connect(saveButton,SIGNAL(clicked()), this, SLOT(Save())); QObject::connect(saveButton,SIGNAL(clicked()), summary, SLOT(clear())); QPushButton* discardButton = new QPushButton("Discard"); QObject::connect(discardButton,SIGNAL(clicked()), summary, SLOT(clear())); QObject::connect(discardButton,SIGNAL(clicked()), this, SLOT(discard())); startDate->setDateTime( QDateTime::currentDateTime() ); finishDate->setDateTime( QDateTime::currentDateTime() ); finishDate->setDateTime(startDate->dateTime().addSecs( 3600 )); QStringList locationList; locationList << "Enaic" << "Dec10" << "Active"; QComboBox * location = new QComboBox(); location->addItems(locationList); //Set Buddies startLabel->setBuddy(startDate); finishLabel->setBuddy(finishDate); summaryLabel->setBuddy(summary); locationLabel->setBuddy(location); QGridLayout * upperlayout = new QGridLayout; upperlayout->addWidget(startLabel, 0, 0); upperlayout->addWidget(startDate, 0, 1); upperlayout->addWidget(finishLabel, 1, 0); upperlayout->addWidget(finishDate, 1, 1); upperlayout->addWidget(locationLabel, 2, 0); upperlayout->addWidget(location, 2, 1); QVBoxLayout * middleLayout = new QVBoxLayout(); middleLayout->addWidget(summaryLabel); middleLayout->addWidget(summary); QHBoxLayout * bottomLayout = new QHBoxLayout(); bottomLayout->addWidget(saveButton); bottomLayout->addWidget(discardButton); QVBoxLayout * mainLayout = new QVBoxLayout(); mainLayout->addLayout(upperlayout); mainLayout->addLayout(middleLayout); mainLayout->addLayout(bottomLayout); void Press::Save(){ QString filename="test.txt"; QFile file(filename); if (file.open(QIODevice::WriteOnly)) { QTextStream stream(&file); QString startboxstring = startbox->dateTime().toString("dd.MM.yyyy hh.mm"); QString finishboxstring = finishbox->dateTime().toString("dd.MM.yyyy hh.mm"); QString locationboxstring = locationbox->currenttext(); QString summaryboxstring = summarybox->toPlainText(); stream << startboxstring.toAscii()<<"/n"<< finishboxstring.toAscii()<<"/n"<<locationboxstring.toAscii()<<"/n"<<summaryboxstring.toAscii(); } locationbox->setCurrentIndex(0); startbox->setDateTime(QDateTime::currentDateTime()); finishbox->setDateTime(QDateTime::currentDateTime().addSecs(3600)); } void Press:Discard() { locationbox->setCurrentIndex(0); startbox->setDateTime(QDateTime::currentDateTime()); finishbox->setDateTime(QDateTime::currentDateTime().addSecs(3600)); } window->setLayout(mainLayout);
}
@If you look at the discardbutton part, this is where the problem is! Im sure I have implemented the constructor..
-
From the code you posted, it seems you are both a beginner in C++ and Qt.
There's no constructor (at least in that file), you are implementing two functions from Press inside the main function and you never start the event loop.
You should really take the time to read Qt's examples. You'll go quicker after that.
-
Are you using Qt Creator? If so you should start by adding a new class. Let's call it Press, then Qt creator will create press.h and press.cpp for your. Press. h will look like this:
@class Press : public QWidget
{
Q_OBJECT
public:
explicit Press(QWidget *parent = 0);signals: public slots:
};
@press.cpp will look like this
@
#include "press.h"Press::Press(QWidget *parent) :
QWidget(parent)
{
}
@When you add any private member to your .h file like
@ QPushButton* saveButton;@
You should initialize it in your class constructor's initializer list. That would give you a .h file that now looks like this
@
#include <QWidget>class QPushButton;
class Press : public QWidget
{
Q_OBJECT
public:
explicit Press(QWidget *parent = 0);signals: public slots: private: QPushButton* saveButton;
};
@and a .cpp file that looks like this:
@
#include "press.h"
#include <QPushButton>Press::Press(QWidget *parent) :
QWidget(parent),
saveButton (new QPushButton("Save",this))
{
}
@Now your saveButton pointer has a legitimate value when the class constructor runs. Of course you have to run the class constructor. So somewhere in main if you want to use the Press class you need the include and a statement that looks like this is you want a solid instance of your object.
@
Press myPress;
@THEN when you want to use Save in a connect statement you would use a statement like this:
@
QObject::connect(saveButton,SIGNAL(clicked()), &myPress, SLOT(Save()));
@That line assumes saveButton is a pointer. With savePointer as a solid object main would end up looking something like this
@
Press myPress;
QPushButton saveButton;
QObject::connect(&saveButton,SIGNAL(clicked()), &myPress, SLOT(Save()));
@While what follows is self-promotion I REALLY think you would benefit from watching my "Introduction to Qt":http://bit.ly/introqt course on Pluralsight.
Pluralsight is a subscription service. You have the right to a subscription service If you can't afford a subscription service one will be provided to you!Just send me an email through the forum and I'll give you a VIP pass good for unlimited hours (includes offline viewing) that lasts for one week. My first course is four+ hours long. You should be able to watch both it and my class on "Qt Quick":http://bit.ly/qtquickfun. It should even give you enough time to watch some of the programming classes on C++.
Edit:Fixed memory leaks.
-
[quote author="Rolias" date="1415821602"]
main would end up looking something like this@
Press* myPress = new Press();
QPushButton* saveButton = new QPushButton();
QObject::connect(saveButton,SIGNAL(clicked()), myPress, SLOT(Save()));
@
[/quote]Watch out, there are two memory leaks here
-
Ooops. Sorry. Really three leaks I think. Although IMO myPress and saveButton are purely pedantic leaks since they are in main. The one inside Press is a nasty one. They are all bad form, so I updated the example. Hope, I got it right this time. Thanks for linting my code.