You are not logged in.

1

Thursday, September 14th 2006, 3:46pm

program => main window + button

Here's the program:

box.h

Quoted

#ifndef BOX_H
#define BOX_H
#include <qmainwindow.h>
#include <qpushbutton.h>

class QPushButton;

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow();

private:
QPushButton *button;
};

#endif



box.cpp

Quoted

#include <QtGui>
#include "box.h"

MainWindow::MainWindow()
{
button = new QPushButton("Hello");
}



main.cpp

Quoted

#include "box.h"
#include <QtGui>

int main(int argc,char *argv[])
{
QApplication app(argc,argv);
MainWindow *Wind = new MainWindow;
Wind->show();
return app.exec();
}



And the problem is that the button inside the window does not show (only the mainwindows shows).

moogle

Beginner

  • "moogle" is male

Posts: 21

Location: Sweden

Occupation: Developer

  • Send private message

2

Thursday, September 14th 2006, 5:34pm

You have to declare that the pushbutton is a child of QMainwindow->MainWindow.

Here you have a perfect opportunity to learn the "parent-stuff".

1. Almost everything in QT is a Widget and also is QPushbutton.

Source code

1
2
3
4
5
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {

    // create a pushbutton and pass the pointer to its parent widget.
    button = new QPushButton("This is a button",this);
}


and it will show.

3

Thursday, September 14th 2006, 5:57pm

Hey.

I did this:

Quoted

MainWindow(QWidget *parent=0);

I'll explain it here and you tell me if I'm right.
So, here I declared a constructor of MainWindow class that has a parameter which is a pointer that points to a type QWidget and it's default value is 0. (that means if I was calling the constructor like this "MainWindow()" it would have set it to 0 (what would it mean if it would have been set to 0 -> that this windows is a parent window)?


And I did this:

Quoted

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)


Here I declared a constructor MainWindow to use a pointer to a type QWidget (that gets the value of a parent) ----> and then it passes that value to a QMainWindow constructor. Is that right?

Can I only ask you: which value does it get? --> can you tell me in example?

This post has been edited 1 times, last edit by "eleanor" (Sep 14th 2006, 6:23pm)


moogle

Beginner

  • "moogle" is male

Posts: 21

Location: Sweden

Occupation: Developer

  • Send private message

4

Thursday, September 14th 2006, 6:27pm

Quoted

Originally posted by eleanor
Hey.

I did this:

Quoted

MainWindow(QWidget *parent=0);

And I did this:

Quoted

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)


Here I declared a constructor MainWindow to use a pointer to a type QWidget (that gets the value of a parent) ----> and then it passes that value to a QMainWindow constructor. Is that right?

Yes..

Quoted


Can I only ask you: which value does it get? --> can you tell me in example?


What do you mean by "it"?

This post has been edited 1 times, last edit by "moogle" (Sep 14th 2006, 6:28pm)


5

Thursday, September 14th 2006, 7:56pm

I mean which value does the pointer get? I understand that, just cannot imagine it... A simple example would help me, thank you

I have another example that will help me understand.

I cannot get this to work properly:


box.h

Quoted

#ifndef BOX_H
#define BOX_H
#include <qmainwindow.h>
#include <qpushbutton.h>

class QPushButton;

class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent=0);
private:
QPushButton *button;
};


class MainWindow2 : public MainWindow
{
Q_OBJECT
public:
MainWindow2(MainWindow *parent2=0);
private:
QPushButton *button2;
};


#endif



box.cpp

Quoted

#include <QtGui>
#include "box.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
button = new QPushButton("Hello",this);
}

MainWindow2::MainWindow2(MainWindow *parent2) : MainWindow(parent2)
{
button2 = new QPushButton("Kako_si?",this);
}



main.cpp

Quoted

#include "box.h"
#include <QtGui>

int main(int argc,char *argv[])
{
QApplication app(argc,argv);
MainWindow *Wind = new MainWindow;
Wind->show();
return app.exec();
}

This post has been edited 1 times, last edit by "eleanor" (Sep 15th 2006, 12:54am)