Page 1 of 2 12 LastLast
Results 1 to 20 of 22

Thread: Using gui objectNames when coding

  1. #1
    Join Date
    Dec 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Using gui objectNames when coding

    Hey all,

    Complete noob to QT here. I am trying to write a gui for a program that I wrote in traditional c++ (command line application) - I want to populate a label widget with a value when I push a button on the GUI. The label's objectName is "label" - and my code looks something like this:

    Qt Code:
    1. #include "brachygui.h"
    2. #include "ui_brachygui.h"
    3. #include <QLabel>
    4.  
    5. BrachyGUI::BrachyGUI(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::BrachyGUI)
    8. {
    9. ui->setupUi(this);
    10. }
    11.  
    12. BrachyGUI::~BrachyGUI()
    13. {
    14. delete ui;
    15. }
    16.  
    17. void BrachyGUI::on_pushButton_clicked()
    18. {
    19. label->setText("Monday");
    20. }
    To copy to clipboard, switch view to plain text mode 

    When I build, I get the error "error: 'label' was not declared in this scope". Where do I define the label, or is it already defined somewhere, and I need to access it differently? I've searched the forums and google, but haven't been able to work it out

    Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Using gui objectNames when coding

    If you use designer, then the objects you've added via designer are available via the "ui" object. Example:

    Qt Code:
    1. ui->label->setText("Monday");
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to tbscope for this useful post:

    The physicist (11th December 2010)

  4. #3
    Join Date
    Dec 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Using gui objectNames when coding

    Tbscope, thanks, that's perfect!

    I'm really looking forward to getting to grips with QT, it looks much better than MS Visual Studio (with the added bonus of being x-platform).

  5. #4
    Join Date
    Dec 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Using gui objectNames when coding

    Can anyone help me with a similar thing following on from this? How do I set the value of a label to a variable from elsewhere in the program? I just tried this

    Qt Code:
    1. void gui_brachy::on_pushButton_clicked()
    2. {
    3. function();
    4.  
    5. ui->label->setText(variable);
    6. }
    To copy to clipboard, switch view to plain text mode 

    where variable is in the function. I just tried it but I got an error saying that the variable had not been defined in this scope?

  6. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using gui objectNames when coding

    You'd write it in the same way as typical C++, pass the variable as a parameter, or use a class member variable.

    So you'd have, for example "QString m_svariable" in your class, and just use "ui->label->setText(m_svariable)";

  7. The following user says thank you to squidge for this useful post:

    The physicist (12th December 2010)

  8. #6
    Join Date
    Dec 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Using gui objectNames when coding

    Classes aren't something I've used in my limited experience of C++, so I will go away and read up them now, cheers

  9. #7
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using gui objectNames when coding

    Good idea, you'll be using them a lot in Qt programming

  10. #8
    Join Date
    Dec 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Using gui objectNames when coding

    Just cracked it, thanks. I didn't realise that what I put in setText() has to be of type QString!

  11. #9
    Join Date
    Dec 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Using gui objectNames when coding

    Another day, another problem!

    When I try to access labels via ui->label_X, some of them aren't appearing. I entered the name manually, but when I compile, I get the error " 'class ui_guibrachy' has no member name 'label_X'

    Have I turned something off that I don't know about

    I tried adding a button to see if that showed up, which it didn't. Could it be that I designed the ui on Mac OS X and today I'm using windows?!

    Thanks.

  12. #10
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Using gui objectNames when coding

    When I try to access labels via ui->label_X, some of them aren't appearing. I entered the name manually, but when I compile, I get the error " 'class ui_guibrachy' has no member name 'label_X'
    1. Open this file with designer and double-check the object's names.
    2. Make sure you have saved changes.
    3. Make sure that you compile correct .ui file ( check if its included in your .pro project file ) and include correct ui header in your sources.
    4. Check for typos in source files.
    5. Do a clean build ( make clean, qmake, make [debug/release] ) .

    There is a big chance of success if you do all of this correctly

    I tried adding a button to see if that showed up, which it didn't
    You mean that you've added a button via designer and it wont show up in you app ? If yes, then double-check 2) and 3)

  13. #11
    Join Date
    Dec 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Using gui objectNames when coding

    1. Done
    2. Done (closed and opened again to make sure they're there)
    3. Done (all in the proj file)
    4. Looks OK
    5. Think I did this, Build->clean all

    Any other ideas

  14. #12
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Using gui objectNames when coding

    look in ui_guibrachy.h see if you can spot your label object.
    If not, then some or all of steps 1-3 you didn't do right.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  15. #13
    Join Date
    Dec 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Using gui objectNames when coding

    Somehow, switching to the debug build, and then going back to the release version has updated everything! Does anyone know how this fixed it?
    Last edited by The physicist; 13th December 2010 at 15:50.

  16. #14
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Using gui objectNames when coding

    Without knowing all the details, switching between debug and release probably alters some build files and recompiles the project.

    Always look at the compile output pane (if you use Qt Creator).
    It shows you exactly what it does. If you see that qmake didn't run because it thinks it doesn't need to, that's a first clue.
    Then see if all the files that need processed, like .ui files, resource files and .h and .cpp files (moc) get processed.

  17. #15
    Join Date
    Dec 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Using gui objectNames when coding

    Quote Originally Posted by tbscope View Post
    Always look at the compile output pane (if you use Qt Creator)
    This is what prompted me to try switching, sound advice!

  18. #16
    Join Date
    Dec 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Using gui objectNames when coding

    I'm aware that I am like a boomerang

    I'm trying to add a logo to my ui. So I added a Graphics View widget (named graphicsView), but this as far as I've got to anything useful other than
    Qt Code:
    1. QImage logo("logo.jpg")
    To copy to clipboard, switch view to plain text mode 
    , I did manage to get it displaying as an icon on a button, although this isn't what I wanted. I'm finding the QGraphicsView documentation confusing and seeing does anyone have any pointers? Thanks.

  19. #17
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Using gui objectNames when coding

    For a simple jpg viewer, you can also use QLabel.
    http://doc.qt.nokia.com/4.7/qlabel.html#pixmap-prop

    Graphics view is more geared towards complex scenes.

  20. #18
    Join Date
    Dec 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Using gui objectNames when coding

    Thanks, will have a go with this...

    Thanks, will have a go with this...


    Added after 50 minutes:


    Perfect, thanks again. For anyone else interested, add a pixmap and painter variable to the gui's header file
    Qt Code:
    1. private:
    2. Ui::gui_brachy *ui;
    3. QPixmap m_pixmap;
    4. QPainter m_painter;
    To copy to clipboard, switch view to plain text mode 
    then add the variables (+a file) like below, and add the line below the setupUi line
    Qt Code:
    1. gui_brachy::gui_brachy(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::gui_brachy),
    4. m_pixmap("logo.jpg"),
    5. m_painter(&m_pixmap)
    6. {
    7. ui->setupUi(this);
    8. ui->label->setPixmap(m_pixmap);
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 

    When I do this, even though the label's position is on the left in the editor, it loads the picture on the right and cuts off some of the image. Is there an autoscale function? And why does it move the label to the other side of the ui?
    Last edited by The physicist; 16th December 2010 at 21:25.

  21. #19
    Join Date
    Dec 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Using gui objectNames when coding

    Bump - does anyone know about an autoscale function and why the label position gets moved?

  22. #20

    Default Re: Using gui objectNames when coding

    Hello guys. I'm working with QT and it's not the first day, when i can't find how to solve my problem. It's not big. I have a lot buttons named p1, p2, p3 and more... the only thing - i want to use numbers via for(int i =0......).
    is it possible? ang how?
    for (int i=0; i<16; i++)
    {
    ui->pi->setText("something"); <--- here i is my variable...
    }
    can anyone help me?
    Thanks

Similar Threads

  1. Coding a game with Qt
    By seltra in forum Qt Programming
    Replies: 5
    Last Post: 5th October 2010, 20:49
  2. VC++ 9.0 coding Symbian ?
    By nhs_0702 in forum Qt Programming
    Replies: 0
    Last Post: 3rd May 2010, 03:48
  3. Replies: 6
    Last Post: 29th January 2010, 23:56
  4. Replies: 0
    Last Post: 18th July 2009, 13:07
  5. What is your coding style ??
    By guestgulkan in forum General Discussion
    Replies: 9
    Last Post: 29th May 2006, 09:22

Tags for this Thread

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.