How do I call the constructor of a class' member? - c++

I wanted to declare a QGraphicsView which has a previously declared member as an argument of the constructor, but the compiler interprets it as a function.
(Code for reference)
class Widnow : public QMainWindow
{
Q_OBJECT
// constructors, member functions, etc...
private:
Ui::Widnow *ui;
QTimer timer01;
QGraphicsScene gaem;
QGraphicsView wiev(&gaem); //this gets interpreted as a function
}
Trying to call the constructor as QGraphicsView wiev = QGraphicsView(&gaem); also causes an error, as the copy constructor has been deleted... Is there a way to declare this member without errors?

The C++ compiler thinks that it is a method declaration where "wiev" as method that returns an instance of QGraphicsView. Use this way
class Widnow : public QMainWindow
{
private:
QGraphicsScene gaem;
QGraphicsView wiev; //this gets interpreted as a function
public:
Widnow() : wiev(&gaem)
{}
};

Related

Declaring class members

I need to create a signal/slot connection in Qt (old syntax). In order to do that, I need to create a class. In that class, some members must be declared. Those members are: a variable doverka which is a QString and text which is a QTextEdit widget. The question is: how on earth do I declare them? Pardon me for the stupid question but I am new to classes. For me, declaring those is not that simple. I have already declared a member function and created a connection, but as the doverka and the text have not yet been declared, nothing works, naturally.
class MyObject : public QObject
{
Q_OBJECT
public: /*how should I declare 'text' and 'doverka' here??? */
public slots:
void onClicked() {
text.setText(doverka);
}
};
QObject::connect(
&btn_t,
SIGNAL(clicked()),
this,
SLOT(onClicked()));

Qt: Access functions of class which is using a GUI widget outside the constructor

I have a Qt-GUI called "mainWindow".
In the constructor I am using a class called "testclass", which uses a widget from the GUI.
mainWindow::mainWindow(QWidget *parent): QMainWindow(parent)
{
testclass window_test(ui.Widget);
window_test.function();
}
Now I would like to access functions of "window_test" outside of the constructor.
I tried to add
testclass window_test(ui.Widget);
as a class member to the mainWindow class but I can't access ui.Widget there.
Whats the best way to handle this situation?
Have you declared the prototypes of your functions in the header file of your mainWindow? It may simply be a scope issue.
I added the instance of "testclass" to mainWindow.h but I had to use a standard constructor for it. Therefore I declared a new function to pass the widget to the class.
If you have to pass a parameter to the constructor of your member variable window_test, you can do it like this:
mainWindow::mainWindow(QWidget *parent) : QMainWindow(parent),
window_test(ui.Widget)
{
}
The declaration of the member variable is simply
class mainWindow {
...
testwindow window_test;
};
If ui is itself a member variable of mainWindow which has a constructor, the order of declaration can be important. ui should then be declared before window_test so it is already constructed when it's used.

In Qt MainWindow, why does an instance have a pointer to an instance?

The MainWindow code generated by QtCreator says:
namespace Ui {
class MainWindow; // forward-declare Ui::MainWindow (?)
}
class MainWindow : public QMainWindow // Declare MainWindow class (Ui::MainWindow?)
{
Q_OBJECT
public:
explicit MainWindow( QWidget *parent = 0 );
/**/ ~MainWindow( void );
// ...
private:
Ui::MainWindow *ui;
// ...
};
main() does:
MainWindow w;
w.show( );
MainWindow::MainWindow( QWidget *parent ) does:
ui( new Ui::MainWindow ) // Initialization
I don't understand why a MainWindow instance has a pointer to another/a different/a new MainWindow in its ui instance variable. I instrumented the MainWindow::MainWindow constructor, and I can see it's only being called once. So presumably that's the automatic variable on the stack in main(). But what about the ui( new Ui::MainWindow ) that happens in the constructor? That's creating a MainWindow on the heap, isn't it? How is it being initialized?
Maybe the subsequent ui->setupUi( this ) in the constructor is doing some magic? Otherwise, it seems like this would recurse to stack crash, as each new MainWindow creates a new MainWindow to populate its ui instance variable.
The Ui::MainWindow class is code generated by uic from the respective QtDesigner file.
It is not a widget but a helper that contains code to populate a widget.
So in your case it is code that is used to populate a QMainWindow derived class named MainWindow.
Ui::MainWindow is held as a pointer to allow forward declaration and avoid build dependencies of code including MainWindow's header to the generate code (which will change everytime you change something in QtDesigner)
There are two different classes in play here:
::MainWindow
::Ui::MainWindow
I wouldn't go so far as to say they are not related - but they are not the same class.

How to access members of wrapper class from member which is wrapped in class

For ex. I got:
"wrapper.h"
class wrapper : public QWidget
{
Q_OBJECT
public:
Wrapped_class m_class;
private:
QTimer* m_timer;
}
"Wrapped_class.h"
class Wrapped_class
{
public:
Wrapped_class();
public slots:
f(); // slot which is called when m_timer send signal timeout()
}
"Wrapped_class.cpp"
Wrapped_class::Wrapped_class()
{
QOBject::connect(wrapper::m_timer, SIGNAL(timeout()), this, SLOT( f()))
}
I get error that wrapper::m_timer in not accessible
You need a pointer or reference to the class to access it's non static members. Pass a pointer to the wrapped class when it's being wrapped
add something like this to your Wrapped_class:
void Wrapped_class::setWrapper(wrapper *w)
{
m_wrapper = w;
}
and call this function when the object is being wrapped. Initialize m_rapper to nullptr in constructor
Depending on your intent and the design of your system, you can choose:
Pass a pointer or reference of "wrapper" class to "wrapped" class. Be ware, you have to define wrapper class as a friend in order to access private member.
Write a member function of "wrapper" class to deal with the interaction between two classes. (This does not really conform to your restriction, but it is a design alternative.)
m_timer is not a static member so you cant access it like that. In Wrapped_class.cpp you need the instance of wrapped class to use it
Besides the problem with wrapper::m_timer not being static, it is also private which means that Wrapped_class can't access it. You need to make Wrapped_class a friend of wrapper for it to access private members.

Qt "no matching function for call"

I have
no matching function for call to 'saveLine::saveLine()'
error when compiling my application. The construcor is never actually called.
saveLine class definition:
class saveLine
{
public:
saveLine(QWidget *parent);
private:
QPushButton *selectButton, *acceptButton;
QLabel *filePath;
QLineEdit *allias;
};
saveLine is used in another class which is defined as follows:
class MWindow : public QWidget
{
Q_OBJECT
public:
MWindow(QWidget *parent=0);
private:
saveLine line1;
};
error points to MWindow constructor implementation
MWindow::MWindow(QWidget *parent):QWidget(parent)
{
this->setWindowTitle("Launcher");
this->resize(600,600);
}
What should i do? I intend to use saveLine class in a vector, to create lines at runtime.
EDIT: i've misdeclared line1, it should read
saveLine *line1;
but now it gives another error
ISO C++ forbids declaration of 'saveLine' with no type
and
expected ';' before '*' token
on this line. It seems like saveLine is no longer considered a class, how so?
Since you provide a user-declared constructor for the class saveLine, the default constructor is not provided by the compiler. Your constructor is not a default constructor (it has one required parameter), so you cannot default construct an object of type saveLine.
Since you have a saveLine object in your MWindow class, you need to initialize it using your constructor, in the MWindow constructor's initializer list:
MWindow::MWindow(QWidget *parent)
: QWidget(parent), line1(parent)
{
//...
}
(I'm assuming that parent pointer is the one you mean to pass; if you need to give it something else, then give it what it needs)
Another option would be to provide a default argument for the parameter in saveLine's constructor:
saveLine(QWidget *parent = 0);
This would allow that constructor to be called with no arguments (and would make it a default constructor). Whether this makes sense to do depends on whether the parent pointer really is optional. Obviously, if you do this, you'll need to check to be sure the pointer is not null before you dereference and use it.
but now it gives another error
ISO C++ forbids declaration of
'saveLine' with no type
You need to add a forward declaration to tell the compiler that saveLine class exists:
Like this:
//declare that there will be a class saveLine
class saveLine;
class MWindow : public QWidget
{
Q_OBJECT
public:
MWindow(QWidget *parent=0);
private:
saveLine *line1;
};
You have to call the constructor of saveLine in the constructor of MWindow giving him the desired parent.
Use:
MWindow::MWindow(QWidget *parent) : QWidget(parent), line1(parent)
{
this->setWindowTitle("Launcher");
this->resize(600,600);
}
You are declaring an instance of saveLine in the class declaration, instead of a pointer to a saveLine.
You could change the reference in MWindow to
saveLine* line1;
OR
you could implement like this:
MWindow::MWindow(QWidget *parent):QWidget(parent), line1(parent)
{
this->setWindowTitle("Launcher");
this->resize(600,600);
}
Try adding class saveLine; just above the class MWindow line. This often occurs when the .h files for the saveLine class and the MWindow class include each other (either directly or indirectly). For example, see the first few posts of http://www.allegro.cc/forums/thread/594307