Results 1 to 14 of 14

Thread: QWidget application does not display anything

  1. #1
    Join Date
    Mar 2013
    Location
    Hyderabad,Bangalore,India
    Posts
    70
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QWidget application does not display anything

    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
    ----------------

    Qt Code:
    1. #include "apphub.h"
    2. #include<QDebug>
    3.  
    4.  
    5. AppHub::AppHub(QWidget *parent) : QWidget(parent)
    6. {
    7. qDebug() << "AppHub";
    8.  
    9. Mainwidget = new QWidget;
    10. MainLayout = new QGridLayout;
    11. Mainwidget->setLayout(MainLayout);
    12.  
    13.  
    14. //Mainwidget->setPalette(QPalette(QColor(255,0,0,255)));
    15. //Mainwidget->setAutoFillBackground(true);
    16. Mainwidget->setMinimumSize(500,500);
    17.  
    18. for(int r=0;r<1;r++) {
    19. for(int c=0;c<2;c++) {
    20. App = new AppRsrc;
    21. MainLayout->addWidget(App,r,c,10,2,Qt::AlignTop);
    22. }
    23. }
    24. //Mainwidget->show();
    25. // showing the window seperately , but without this also it should show
    26. }
    27.  
    28. AppHub::~AppHub()
    29. {
    30.  
    31. }
    32.  
    33. AppRsrc:: AppRsrc()
    34. {
    35. qDebug() << "AppRsrc";
    36. AppWidget = new QWidget ;
    37. AppButton = new QPushButton;
    38. AppLayout = new QVBoxLayout;
    39. AppNameLbl = new QLabel;
    40. AppWidget->resize(98,97);
    41. AppWidget->setPalette(QColor(255,0,0,255));
    42. AppWidget->setAutoFillBackground(true);
    43. QPixmap pixmap(":/appimages/orca.png");
    44. QIcon AppIcon(pixmap);
    45. AppButton->setFixedSize(100,100);
    46. AppButton->setIcon(AppIcon);
    47. AppNameLbl->setFixedSize(83,20);
    48.  
    49. AppLayout->addWidget(AppButton);
    50. AppLayout->addWidget(AppNameLbl);
    51.  
    52. AppWidget->setLayout(AppLayout);
    53. }
    54.  
    55. AppRsrc:: ~AppRsrc()
    56. {
    57.  
    58. }
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. #ifndef APPHUB_H
    2. #define APPHUB_H
    3.  
    4. #include <QWidget>
    5. #include <QPushButton>
    6. #include <QGridLayout>
    7. #include <QLabel>
    8. #include "app_rsrc.h"
    9.  
    10. class AppRsrc;
    11. class AppHub : public QWidget
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. AppHub(QWidget *parent = 0);
    17. ~AppHub();
    18.  
    19. private:
    20. QWidget *Mainwidget;
    21. AppRsrc *App;
    22. QGridLayout *MainLayout;
    23.  
    24. };
    25.  
    26. #endif // APPHUB_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. app_rsrc.h
    2. ----------------
    3.  
    4. #ifndef APP_RSRC_H
    5. #define APP_RSRC_H
    6.  
    7. #include<QPushButton>
    8. #include<QLabel>
    9. #include<QGridLayout>
    10. #include<QIcon>
    11. #include<QWidget>
    12.  
    13. class AppRsrc : public QWidget
    14. {
    15. private:
    16. QWidget *AppWidget;
    17. QPushButton *AppButton;
    18. QVBoxLayout *AppLayout;
    19. QLabel *AppNameLbl;
    20.  
    21. public:
    22. AppRsrc();
    23. ~AppRsrc();
    24. };
    25.  
    26. #endif // APP_RSRC_H
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. #include "apphub.h"
    2. #include <QApplication>
    3. #include<QMainWindow>
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. AppHub w;
    8.  
    9. w.show();
    10.  
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

  2. The following user says thank you to PstdEr for this useful post:


  3. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QWidget application does not display anything

    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,
    _

  4. The following 2 users say thank you to anda_skoa for this useful post:

    PstdEr (10th May 2013)

  5. #3
    Join Date
    Mar 2013
    Location
    Hyderabad,Bangalore,India
    Posts
    70
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QWidget application does not display anything

    Its working

    i just added the following code in AppRsrc

    Qt Code:
    1. QGridLayout *templ = new QGridLayout;
    2. setLayout(templ);
    3. templ->addWidget(AppWidget);
    To copy to clipboard, switch view to plain text mode 

    and in AppHub

    Qt Code:
    1. QGridLayout *templ = new QGridLayout;
    2. setLayout(templ);
    3. templ->addWidget(Mainwidget);
    To copy to clipboard, switch view to plain text mode 

    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.

  6. The following user says thank you to PstdEr for this useful post:


  7. #4
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QWidget application does not display anything

    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
    Qt Code:
    1. setStyleSheet("QWidget { background-image: url(:/path/image.png) } ");
    To copy to clipboard, switch view to plain text mode 

    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 ?
    Last edited by rawfool; 10th May 2013 at 07:16.

  8. The following user says thank you to rawfool for this useful post:


  9. #5
    Join Date
    Mar 2013
    Location
    Hyderabad,Bangalore,India
    Posts
    70
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QWidget application does not display anything

    Quote Originally Posted by rawfool View Post
    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
    Qt Code:
    1. MainWidget->setStyleSheet("border-image: url(:/appimages/basketball.png)");
    To copy to clipboard, switch view to plain text mode 

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

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

  10. The following user says thank you to PstdEr for this useful post:


  11. #6
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QWidget application does not display anything

    in my case buttons are used just to display the icon images
    Then 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.

  12. The following user says thank you to rawfool for this useful post:


  13. #7
    Join Date
    Mar 2013
    Location
    Hyderabad,Bangalore,India
    Posts
    70
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QWidget application does not display anything

    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.
    Last edited by PstdEr; 10th May 2013 at 09:52. Reason: add more info

  14. The following user says thank you to PstdEr for this useful post:


  15. #8
    Join Date
    Mar 2013
    Location
    Hyderabad,Bangalore,India
    Posts
    70
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QWidget application does not display anything

    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");
    }

  16. #9
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QWidget application does not display anything

    Can you explain about the functionality you are trying to achieve? Can you brief about what you are trying as a whole ?

  17. #10
    Join Date
    Mar 2013
    Location
    Hyderabad,Bangalore,India
    Posts
    70
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QWidget application does not display anything

    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.

  18. #11
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QWidget application does not display anything

    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
    Qt Code:
    1. #ifndef CWIDGET_H
    2. #define CWIDGET_H
    3.  
    4. #include <QWidget>
    5. #include <QLabel>
    6. #include <QGridLayout>
    7. #include <QPainter>
    8.  
    9. class CWidget : public QWidget
    10. {
    11. Q_OBJECT
    12. private:
    13. void setIcons();
    14.  
    15. QLabel *icon1, *icon2, *icon3;
    16.  
    17. protected:
    18.  
    19.  
    20. public:
    21. CWidget(QWidget *parent = 0);
    22. ~CWidget();
    23. };
    24.  
    25. #endif // CWIDGET_H
    To copy to clipboard, switch view to plain text mode 

    cwidget.cpp
    Qt Code:
    1. #include "cwidget.h"
    2. #include <QPalette>
    3.  
    4. CWidget::CWidget(QWidget *parent)
    5. : QWidget(parent)
    6. {
    7. QPalette palette;
    8. palette.setBrush(QPalette::Window, QBrush(QImage(":/imgs/bckGrnd.png")));
    9. this->setPalette(palette);
    10.  
    11. setIcons();
    12. }
    13.  
    14. CWidget::~CWidget()
    15. {
    16.  
    17. }
    18.  
    19. void CWidget::setIcons()
    20. {
    21. lyt = new QGridLayout();
    22.  
    23. icon1 = new QLabel();
    24. icon1->setStyleSheet("background-image: url(:/imgs/24sata.png); background-repeat: none");
    25. icon2 = new QLabel();
    26. icon2->setStyleSheet("background-image: url(:/imgs/02.png); background-repeat: none");
    27. icon3 = new QLabel();
    28. icon3->setStyleSheet("background-image: url(:/imgs/4EXT_Recovery.png); background-repeat: none");
    29.  
    30. lyt->addWidget(icon1, 0, 0);
    31. lyt->addWidget(icon2, 0, 1);
    32. lyt->addWidget(icon3, 0, 2);
    33.  
    34. this->setLayout(lyt);
    35. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include "cwidget.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. CWidget w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

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

  19. The following user says thank you to rawfool for this useful post:

    PstdEr (14th May 2013)

  20. #12
    Join Date
    Mar 2013
    Location
    Hyderabad,Bangalore,India
    Posts
    70
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QWidget application does not display anything

    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..

  21. #13
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QWidget application does not display anything

    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.

  22. #14
    Join Date
    Mar 2013
    Location
    Hyderabad,Bangalore,India
    Posts
    70
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QWidget application does not display anything

    I used

    button[btno] = 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 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.
    Last edited by PstdEr; 15th May 2013 at 03:38. Reason: added query

Similar Threads

  1. Create And Display QWidget in code
    By ShapeShiftme in forum Qt Programming
    Replies: 4
    Last Post: 19th May 2012, 07:04
  2. Display an image to a plain QWidget
    By franco.amato in forum Qt Programming
    Replies: 4
    Last Post: 19th March 2012, 09:07
  3. Display a QWidget on a remote computer
    By tarod in forum Qt Programming
    Replies: 4
    Last Post: 24th July 2008, 07:41
  4. Display a QWidget on two X-Server at the same time
    By tarod in forum Qt Programming
    Replies: 0
    Last Post: 1st July 2008, 12:55
  5. Replies: 3
    Last Post: 8th March 2007, 14:54

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.