PDA

View Full Version : QWidget application does not display anything



PstdEr
9th May 2013, 15:22
I have written the following program using QWidget

and placed some pushbuttons and labels in layout and added those in another layout, but nothing is getting displayed except some default window.

Must i have QMainwindow always as a base widget?

please help.

apphub.cpp
----------------



#include "apphub.h"
#include<QDebug>


AppHub::AppHub(QWidget *parent) : QWidget(parent)
{
qDebug() << "AppHub";

Mainwidget = new QWidget;
MainLayout = new QGridLayout;
Mainwidget->setLayout(MainLayout);


//Mainwidget->setPalette(QPalette(QColor(255,0,0,255)));
//Mainwidget->setAutoFillBackground(true);
Mainwidget->setMinimumSize(500,500);

for(int r=0;r<1;r++) {
for(int c=0;c<2;c++) {
App = new AppRsrc;
MainLayout->addWidget(App,r,c,10,2,Qt::AlignTop);
}
}
//Mainwidget->show();
// showing the window seperately , but without this also it should show
}

AppHub::~AppHub()
{

}

AppRsrc:: AppRsrc()
{
qDebug() << "AppRsrc";
AppWidget = new QWidget ;
AppButton = new QPushButton;
AppLayout = new QVBoxLayout;
AppNameLbl = new QLabel;
AppWidget->resize(98,97);
AppWidget->setPalette(QColor(255,0,0,255));
AppWidget->setAutoFillBackground(true);
QPixmap pixmap(":/appimages/orca.png");
QIcon AppIcon(pixmap);
AppButton->setFixedSize(100,100);
AppButton->setIcon(AppIcon);
AppNameLbl->setFixedSize(83,20);

AppLayout->addWidget(AppButton);
AppLayout->addWidget(AppNameLbl);

AppWidget->setLayout(AppLayout);
}

AppRsrc:: ~AppRsrc()
{

}


apphub.h
----------------



#ifndef APPHUB_H
#define APPHUB_H

#include <QWidget>
#include <QPushButton>
#include <QGridLayout>
#include <QLabel>
#include "app_rsrc.h"

class AppRsrc;
class AppHub : public QWidget
{
Q_OBJECT

public:
AppHub(QWidget *parent = 0);
~AppHub();

private:
QWidget *Mainwidget;
AppRsrc *App;
QGridLayout *MainLayout;

};

#endif // APPHUB_H





app_rsrc.h
----------------

#ifndef APP_RSRC_H
#define APP_RSRC_H

#include<QPushButton>
#include<QLabel>
#include<QGridLayout>
#include<QIcon>
#include<QWidget>

class AppRsrc : public QWidget
{
private:
QWidget *AppWidget;
QPushButton *AppButton;
QVBoxLayout *AppLayout;
QLabel *AppNameLbl;

public:
AppRsrc();
~AppRsrc();
};

#endif // APP_RSRC_H


main.cpp
----------------



#include "apphub.h"
#include <QApplication>
#include<QMainWindow>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
AppHub w;

w.show();

return a.exec();
}

anda_skoa
9th May 2013, 15:52
Both AppHub and AppRsrc have no layout.

My gues is that instead of MainWidget and AppWidget you just should be using the widget you are in, i.e. "this"

Cheers,
_

PstdEr
10th May 2013, 03:35
Its working :)

i just added the following code in AppRsrc



QGridLayout *templ = new QGridLayout;
setLayout(templ);
templ->addWidget(AppWidget);



and in AppHub



QGridLayout *templ = new QGridLayout;
setLayout(templ);
templ->addWidget(Mainwidget);



can you also please hint me on following things

1) how to reduce the horizantal and vertical space between the appwidgets.
i want the space to be constant and mininmum no matter the main window is maximized or minimized.

2) how to apply wallpaper to the MainWidget
instead of using some default background or black background i want to use some wallpaper on my mainwidget
i tired to use styleSheet but the image is repeating on buttons also.

and last

i am in the process of developing a UI for my project
the basic aim of the UI is to show set of images , each image represents some app, up on click on image app should be launched.
the appwidgets(buttons to place images and lable for name of app rspt..) here represents the application launchers.

my question is how this kind of UIs are devoloped generally, the way how i started(with some pushbuttons ,labels and icons combined in a grid etc...) is ok or i can be still better.

please answer.

rawfool
10th May 2013, 07:08
1) how to reduce the horizantal and vertical space between the appwidgets.
i want the space to be constant and mininmum no matter the main window is maximized or minimized.
use templ.setspacing()


2) how to apply wallpaper to the MainWidget
try

setStyleSheet("QWidget { background-image: url(:/path/image.png) } ");


the appwidgets(buttons to place images and lable for name of app rspt..) here represents the application launchers.
Use QToolButton if you want use an image. What kind if applications are those, which get launched on click of buttons ?

PstdEr
10th May 2013, 08:20
use QToolButton if you want use an image. What kind if applications are those, which get launched on click of buttons ?

Actually for my UI, Buttons are not at all required because i am not clicking on the button to lauch an application.

I am using buttons becuase i have not found another way to have some image on it(except Qwidget).

in my case buttons are used just to display the icon images
basically i will be navigating and launching through icons based on some external events.


coming to StyleSheets , i used in the same way u suggested but the image is repeatedly pasted on Pushbuttons , layouts and labels etc.

i tried to use


MainWidget->setStyleSheet("border-image: url(:/appimages/basketball.png)");


i have attached the jpg which shows the look of UI.

is there any way to stop repeating the border image on other widgets.

rawfool
10th May 2013, 08:54
in my case buttons are used just to display the icon imagesThen try QLabel.


is there any way to stop repeating the border image on other widgets
If your button is having parent widget then try to make it an independent widget and just add to layout.

PstdEr
10th May 2013, 09:50
I tried QLabel using pixmap and its working.

but i dont quite understand the second point.
do mean to add the widget to the same layout as buttons.

if possible can you modify my code a bit and show.

is there any extra advantage of QLable than QPushButton, i mean with respect to functionalites for setting and getting properties.

thanks in advance.

PstdEr
13th May 2013, 07:25
OK.

I tried with the following, for my background, its working fine now.

i mean no repeated background on pushbuttons and others, but the quality of paint is really poor.

how will i make it more clear and quality.

void AppHub :: paintEvent(QPaintEvent *e)
{
QRect rect(0,0,550,530);
QPainter paint(this);
paint.drawImage(rect,QImage(":/appimages/img.jpg");
//paint.drawIPixmap(0,0,550,530,QImage(":/appimages/img.jpg");
}

rawfool
13th May 2013, 10:42
Can you explain about the functionality you are trying to achieve? Can you brief about what you are trying as a whole ?

PstdEr
14th May 2013, 05:26
Thanks for asking that question.

Can you brief about what you are trying as a whole ?
I would like to create a UI where in it shows bunch of icons , each icon represents an app(mediaplayer,browser,game etc...) just like the UI in mobile phones or any standard desktop.
Lauching of an app is done by click but not directly on the icon there is a special keyboard.

All my apps are developed using QProcess , and i have my apps working, though they are very basic apps.

Can you explain about the functionality you are trying to achieve?

Currently the background of my UI is black , i tried to use some wallpaper with stylesheet functionality, but thats having the problem of repeating on child widgets, so i asked here thinking most of the people might have already done that.

I am not a regular programmer in Qt , actually i am from C background , for this particular project i need to use bit of Qt(C++).

Thanks for showing interest again.

rawfool
14th May 2013, 07:02
What I understood is that you want to set a background image to your widget and place some icons on it. Your problem is that the icons are repeating after setting in QGridLayout. In this case do the following:

cwidget.h

#ifndef CWIDGET_H
#define CWIDGET_H

#include <QWidget>
#include <QLabel>
#include <QGridLayout>
#include <QPainter>

class CWidget : public QWidget
{
Q_OBJECT
private:
void setIcons();

QLabel *icon1, *icon2, *icon3;
QGridLayout *lyt;

protected:


public:
CWidget(QWidget *parent = 0);
~CWidget();
};

#endif // CWIDGET_H

cwidget.cpp

#include "cwidget.h"
#include <QPalette>

CWidget::CWidget(QWidget *parent)
: QWidget(parent)
{
QPalette palette;
palette.setBrush(QPalette::Window, QBrush(QImage(":/imgs/bckGrnd.png")));
this->setPalette(palette);

setIcons();
}

CWidget::~CWidget()
{

}

void CWidget::setIcons()
{
lyt = new QGridLayout();

icon1 = new QLabel();
icon1->setStyleSheet("background-image: url(:/imgs/24sata.png); background-repeat: none");
icon2 = new QLabel();
icon2->setStyleSheet("background-image: url(:/imgs/02.png); background-repeat: none");
icon3 = new QLabel();
icon3->setStyleSheet("background-image: url(:/imgs/4EXT_Recovery.png); background-repeat: none");

lyt->addWidget(icon1, 0, 0);
lyt->addWidget(icon2, 0, 1);
lyt->addWidget(icon3, 0, 2);

this->setLayout(lyt);
}

main.cpp

#include "cwidget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
CWidget w;
w.show();

return a.exec();
}

I've set background image in palette. You can change it in PaintEvent instead. Hope this helps.

PstdEr
14th May 2013, 10:44
Thanks a lot ,it looks simple.
I need to add some code to it for for dimming icons when selected and also add name under the icon.
I hope it is what i needed, i cant try this now , there is some internal problem.
I found out from your profile that you are from bangalore-India.

Can i PM my code to you after modification if i have any problems.

Thank you very much..

rawfool
14th May 2013, 14:29
Since you want to show text along with image, I still suggest you to use QToolButton even if you are not going to click on it directly. Using QToolButton, you can show Image-only/Text-Only/Image-Text modes. Also on trigger of some app/process which is related to a particular icon, you can invoke void QAbstractButton::click() or void QAbstractButton::animateClick( int msec = 100 ) slot to actually make it behave as clicked. And if you want to do some dimming, then write a stylesheet to change it's on-click properties.

PstdEr
15th May 2013, 03:33
I used

button = new QToolButton;
button[btno] ->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
button[btno] ->setText("Game"+ QString :: number(++i));
and its working fine , so i need not to use any extra labels for showing name.

i still have some issues with[B] background and spacing between icons, the code you gave is displaying background image in tile fashion for the widget, if i use paint the image is not clear(kind of blurry and does not look like original image), so i need to find a way to have more clear background.

i will get back after some re-search.

Thanks for help.

And if you want to do some dimming, then write a stylesheet to change it's on-click properties.

is this how we generally do it or its just another way. i jast want some dotted lines around the button and button should be dimmed in some light color when selected.