I'm making a tetris. Well, my glass (QtGlass.h) creates a figure.
I would like to use a parameter here to specify which shape the figure should
take.
Could you suggest me why parameters cause this error:
QtGlass.h:29:23: error: expected identifier before 'L'
QtGlass.h:29:23: error: expected ',' or '...' before 'L'
I've shown in the comments below where this error occurs.
By the way, if I uncomment the lines which signigy a parameterless variant,
it works.
**Figure.h**
class Figure : public QObject {
Q_OBJECT
...
public:
Figure(char Shape);
//Figure();
...
};
**Figure.cpp**
Figure::Figure(char Shape) {
//Figure::Figure() {
previous_shape = 1;
colour = RED;
...
}
**QtGlass.h**
class QtGlass : public QFrame {
Q_OBJECT
...
protected:
Figure the_figure('L'); //QtGlass.h:29:23: error: expected identifier before 'L' QtGlass.h:29:23: error: expected ',' or '...' before 'L'
//Figure the_figure;
...
};
Edded later
When I use this:
class QtGlass : public QFrame {
Q_OBJECT
QtGlass() : the_figure('L') {}
I get this:
QtGlass.cpp:164:50: error: no matching function for call to 'Figure::Figure()'
QtGlass.cpp:164:50: note: candidates are:
Figure.h:38:5: note: Figure::Figure(char)
Figure.h:38:5: note: candidate expects 1 argument, 0 provided
Figure.h:20:7: note: Figure::Figure(const Figure&)
Figure.h:20:7: note: candidate expects 1 argument, 0 provided
QtGlass.cpp
QtGlass::QtGlass(QWidget *parent) : QFrame(parent) {
key_pressed = false;
coord_x = 5;
coord_y = 5;
arrow_n = 0;
highest_line = 21;
this->initialize_glass();
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(moveDownByTimer()));
timer->start(1000);
}
You can't initialize a member object using that syntax. If your compiler supports C++11's uniform initialization syntax or in-class initialization of member variables you can do this:
class QtGlass : public QFrame {
Q_OBJECT
...
protected:
Figure the_figure{'L'};
// or
Figure the_figure = 'L'; // works because Figure(char) is not explicit
...
};
Otherwise you need to initialize the object within QtGlass' constructor initializer list
class QtGlass : public QFrame {
Q_OBJECT
...
protected:
Figure the_figure;
...
};
// in QtGlass.cpp
QtGlass::QtGlass(QWidget *parent)
: QFrame(parent)
, the_figure('L')
{}
You are trying to create an instance of Figure in the definition of QtGlass which is not allowed. You have to instantiate the_figure in the constructor of QtGlass:
class QtGlass : public QFrame {
Q_OBJECT
QtGlass() {
the_figure = Figure('L');
};
...
protected:
Figure the_figure;
...
};
Related
i have 2 class : Class MaFentre and Code
code.h :
class Code : public QObject {
public :
explicit Code(Q3DScatter *scatter);
public slots:
std::vector<point> readingData(std::string inputFileName);
}
MaFenetre.h :
class MaFenetre : public QWidget
{ Q_OBJECT
public:
MaFenetre();
private:
QLineEdit *entry1;
}
Code.cpp :
std::vector<point> Code::readingData(std::string inputFileName){
// i read a file here
}
i created the Code class object in the constructor of the class MaFenetre
Code *modifier = new Code(graph);
for making connection between slot and signal
QObject::connect(entry1, SIGNAL(textChanged(QString)),modifier, SLOT(readingDara(std::string inputFileName)))
i know the parameters must be of the same type , for that i try to code :
QObject::connect(entry, SIGNAL(textChanged(QString.toStdString)),modifier, SLOT(readingDara(std::string inputFileName)))
but it doesnt work
Your signal and slot arguments are not compatible.
You can do this workaround with the lambda function
Code *modifier = new Code();
MaFenetre * poMaFenetre = new MaFenetre();
connect(poMaFenetre->Entry(), &QLineEdit::textChanged,
[modifier](const QString & oText)
{
std::vector<int> data = modifier->readingData(oText.toStdString());
// Handle data here...
});
In the MaFenetre
class MaFenetre : public QWidget
{
Q_OBJECT
public:
MaFenetre() {entry1.reset(new QLineEdit());}
QLineEdit *Entry() {return entry1.data();}
private:
QScopedPointer<QLineEdit> entry1;
};
Using signals and slots it's not the same as calling function and pass parameters.
At first signal and slot must have same parameters type, means they must be defined with same parameters. In your case you have to change your slot to fit possible signals. Also note that returned value is useless in case of slot invoking, so better way is to keep you reading function as is, move it to private area, and create wrapper slot:
void Code::readingDataSlot(QString inputFileName)
{
std::vector<point> result = readingData( inputFileName.toStdString() );
// Do what ever you need with result vector
}
and connect it to signal.
connect(entry1, SIGNAL(textChanged(QString)),modifier, SLOT(readingDataSlot(QString)));
I am getting this error :
/MyApp/company.cpp:3: error: no matching function for call to ‘DepList::DepList(Company*, Company*)’
Company::Company(QObject *parent) : QObject(parent),dep_cache(this,this),search_results(this,this)
^
/MyApp/company.h:14: In file included from ../../../../MyApp/company.h:14:0,
/MyApp/company.cpp:1: from ../../../../MyApp/company.cpp:1:
/MyApp/deplist.h:34: candidate: DepList::DepList(QAbstractItemModel*, Company*)
explicit DepList(QAbstractItemModel *parent = 0,Company* p_company=0);
/MyApp/deplist.h:34: note: no known conversion for argument 1 from ‘Company*’ to ‘QAbstractItemModel*’
^
when compiling the constructor code for Company class:
Company::Company(QObject *parent) : QObject(parent),dep_cache(this,this),search_results(this,this)
{
// .... some intialization stuff here, unrelated to the error
}
and it only happens with classes where the parent is QAbstractItemModel. With other classes , parents of QObject I am using initialization lists without errors. The error appeared when I added initialization lists.
this is how search_results is declared inside Company class:
class Company : public QObject {
...
DepList search_results;
...
}
and it's constructor is:
DepList::DepList(QAbstractItemModel *parent,Company* p_company) : QAbstractItemModel(parent)
{
company=p_company;
}
What is the problem here , and why it only happens with QAbstractItemModel?? QAbstractItemModel is also a child of QObject and I have no problems with initialization lists in QObject classes. I had a thought of casting , but would it be safe ?
Company is a QObject-derived:
class Company : public QObject {
Q_OBJECT
...
explicit Company(QObject *parent = 0);
...
};
But DepList is a QAbstractItemModel-derived:
class DepList : public QAbstractItemModel
{
...
explicit DepList(QAbstractItemModel *parent = 0,Company* p_company=0);
...
}
I am attempting to subclass a QLabel using a header file and I get the error on constructor
IntelliSense: indirect nonvirtual base class is not allowed
class foo : public QLabel
{
Q_OBJECT
foo(QWidget* parent = 0) : QWidget(parent)
{
}
void mouseMoveEvent( QMouseEvent * event )
{
//Capture this
}
};
Any suggestions why this is happening and how I can fix it ?
The problem is here:
foo(QWidget* parent = 0) : QWidget(parent)
You are inheriting from QLabel, but you specify QWidget for the base. You should write this intead:
explicit foo(QWidget* parent = Q_NULLPTR) : QLabel(parent)
// ^^^^^^
Also, please use explicit for that constructor as well as Q_NULL_PTR or at least NULL instead of 0.
I'm trying to init my private variable "DressMen black" into class Dialog/QT
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent=0,DressMen b);
~Dialog();
........
public slots:
........
private slots:
.............
private:
Ui::Dialog *ui;
..........
DressMen black;
..........
};
And my DressMen class .h is
class DressMen {
public:
DressMen();
~DressMen(void);
DressMen(std::string name);
.................
}
DressMen.cpp is
DressMen::DressMen()
{
//set values for default constructor
..................
}
DressMen::DressMen(std::string name){
setType(name);
if(name=="black1"){
......
}
}
Now in my Dialog::Dialog I want init my DressMen black1 private variable equal to
- DressMen black("black1");
so I write
Dialog::Dialog(QWidget *parent,DressMen b) : QDialog(parent),ui(new Ui::Dialog),black(b("black1")) {
.....
}
but the compiler error me
dialog.h:23: error: default argument missing for parameter 2 of 'Dialog::Dialog(QWidget*, DressMen)'
explicit Dialog(QWidget *parent=0,DressMen b);
I don't understand because I'm in practice for C++ ......
thanks
As indicated by Brian Bi, you must either specify a default value for DressMen or you must make your DressMen the first argument. You could do the following:
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent=0, DressMen b = DressMen("black1"));
...
};
Or you could do this:
// Dialog.h
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent=0, DressMen b = DressMen());
...
};
// DressMen.h
class DressMen
{
public:
DressMen(std::string name = "black1");
...
};
The solution that makes the most sense depends on how you expect Dialog and DressMen to be used.
it should be really easy, but something just doesn't work and i can't find the problem.
I have two classes, like below:
1) individualtab
#ifndef INDIVIDUALTAB_H
#define INDIVIDUALTAB_H
#include <QMainWindow>
#include <QInputDialog>
#include <QTableWidget>
#include <QVBoxLayout>
#include <QLabel>
class IndividualTab : public QWidget
{
Q_OBJECT
public:
IndividualTab(QWidget *parent = 0);
QTableWidget *table;
QVBoxLayout *layout;
};
#endif // INDIVIDUALTAB_H
IndividualTab::IndividualTab(QWidget *parent) : QWidget()
{
table = new QTableWidget(0,4);
layout = new QVBoxLayout();
}
2) secondclass
#ifndef SECONDCLASS_H
#define SECONDCLASS_H
#include "individualtab.h"
class secondClass : public QMainWindow
{
Q_OBJECT
public:
explicit secondClass(QWidget *parent = 0);
Ui::secondClass *ui;
~secondClass();
QList<IndividualTab> *individualTabList;
};
#endif // SECONDCLASS_H
secondClass::secondClass(QWidget *parent) : QMainWindow(parent), ui(new Ui::secondClass)
{
ui->setupUi(this);
}
secondClass::~secondClass()
{
delete ui;
}
void secondClass::addNewItem()
{
//Getting parameters
QList<QString> parameters;
//creating QList
//Updating individualTab
for(int i = 0; i < ui->tabWidget->count(); i++)
{
if(parameters.at(0) == ui->tabWidget->tabText(i))
{
IndividualTab tab = individualTabList->at(i);
tab.addItem(parameters);
break;
}
}
}
When i compile i have this error:
In file included from ../secondclass.h:5:0,
from ../secondclass.cpp:1:
/usr/include/qt4/QtGui/qwidget.h: In copy constructor 'IndividualTab::IndividualTab(const IndividualTab&)':
/usr/include/qt4/QtGui/qwidget.h:806:5: error: 'QWidget::QWidget(const QWidget&)' is private
../individualtab.h:10:7: error: within this context
../secondclass.cpp: In member function 'void SecondClass::addNewItem()':
../secondclass.cpp:142:56: note: synthesized method 'IndividualTab::IndividualTab(const IndividualTab&)' first required here
I tought that the problem was in IndividualTab tab = individualTabList->at(i);
So i changed in
IndividualTab *tab = new IndividualTab();
tab = individualTabList->at(i);
but in this case, i had this error:
../secondclass.cpp: In member function 'void SecondClass::addNewItem()':
../secondclass.cpp:143:42: error: cannot convert 'const IndividualTab' to 'IndividualTab*' in assignment
../secondclass.cpp:144:17: error: request for member 'addItem' in 'tab', which is of non-class type 'IndividualTab*'
In file included from ../secondclass.h:5:0,
from ../secondclass.cpp:1:
/usr/include/qt4/QtGui/qwidget.h: In copy constructor 'IndividualTab::IndividualTab(const IndividualTab&)':
../individualtab.h:10:7: instantiated from 'void QList<T>::node_construct(QList<T>::Node*, const T&) [with T = IndividualTab]'
/usr/include/qt4/QtCore/qlist.h:512:13: instantiated from 'void QList<T>::append(const T&) [with T = IndividualTab]'
../Ripetizioni/secondclass.cpp:112:38: instantiated from here
/usr/include/qt4/QtGui/qwidget.h:806:5: error: 'QWidget::QWidget(const QWidget&)' is private
../individualtab.h:10:7: error: within this context
In file included from /usr/include/qt4/QtCore/qobject.h:50:0,
from /usr/include/qt4/QtGui/qwidget.h:46,
from /usr/include/qt4/QtGui/qmainwindow.h:45,
from /usr/include/qt4/QtGui/QMainWindow:1,
from ../secondclass.h:4,
from ../secondclass.cpp:1:
/usr/include/qt4/QtCore/qlist.h: In member function 'void QList<T>::node_construct(QList<T>::Node*, const T&) [with T = IndividualTab]':
/usr/include/qt4/QtCore/qlist.h:372:58: note: synthesized method 'IndividualTab::IndividualTab(const IndividualTab&)' first required here
thanks in advance!
QWidgets aren't meant to be copied. Your individualTabList should be a container of pointers-to-IndividualTab. And there's no good reason for that member itself to be a pointer. Change the declaration to:
QList<IndividualTab*> individualTabList;
Then you can:
IndividualTab *tab = individualTabList.at(i);