View Full Version : why isnt my button showing?
IsleWitch
7th October 2007, 17:20
Hi,
Im trying to create a A GUI.
In my main, im trying to create the "QUIT" button, but it wont show when i run it. I would appreaciate if anyone would tell me why it wont show.
Here is my code:
#include <QApplication>
#include <QPushButton>
#include <QWidget>
#include "browseimages.h"
class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
};
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
QPushButton *b_quit = new QPushButton(tr("Quit"), this);
connect(b_quit, SIGNAL(clicked()), qApp, SLOT(b_quit()));
}
int main(int argc, char **argv)
{
QApplication app(argc, argv);
BrowseImages window;
window.show();
return app.exec();
}
Thank you.
marcel
7th October 2007, 17:28
Where are you creating/showing an instance of MyWidget?
Also b_quit will not look good. You could add it to a layout to handle its position and size.
IsleWitch
7th October 2007, 18:20
Thanks Marcel,
I forgot to creat an instance of MyWidget. I fixed that, and i also used layout to handle the position and size of my quit button.
But, my "quit" button (b_quit) isnt working. The window wont close when i press "Quit". I would like to know where is the problem.
Here is the fixed code:
#include <QApplication>
#include "browseimages.h"
#include <QPushButton>
#include <QWidget>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <Qlabel>
class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
};
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
QPushButton *b_quit = new QPushButton(tr("Quit"));
QPushButton *b_search = new QPushButton(tr("Search"));
connect(b_quit, SIGNAL(clicked()), qApp, SLOT(b_quit()));
b_search->setFixedHeight(40);
b_quit->setFixedHeight(40);
QVBoxLayout *v_search = new QVBoxLayout;
v_search->setAlignment(Qt::AlignCenter);
QLabel *l_search_img = new QLabel();
l_search_img->setPixmap(QPixmap("images/manual.png"));
v_search->addWidget(l_search_img);
v_search->addWidget(b_search);
QHBoxLayout *h_quit = new QHBoxLayout;
h_quit->addStretch();
h_quit->addWidget(b_quit);
h_quit->addStretch();
QVBoxLayout *layout_vert = new QVBoxLayout;
QHBoxLayout *layout_mid = new QHBoxLayout;
layout_mid->addLayout(v_search);
layout_vert->addLayout(layout_mid);
layout_vert->addSpacing(30);
QHBoxLayout *h_search = new QHBoxLayout;
layout_vert->addLayout(h_search);
layout_vert->addLayout(h_quit);
layout_vert->addSpacing(30);
this->setLayout(layout_vert);
}
int main(int argc, char **argv)
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}
Thanks in advance,
marcel
7th October 2007, 18:25
Your connect is wrong. Should be:
connect(b_quit, SIGNAL(clicked()), qApp, SLOT(quit()));
IsleWitch
7th October 2007, 18:28
Your connect is wrong. Should be:
connect(b_quit, SIGNAL(clicked()), qApp, SLOT(quit()));
Thank you what a silly mistake :o
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.