Results 1 to 20 of 22

Thread: Using gui objectNames when coding

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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

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.