PDA

View Full Version : Using gui objectNames when coding



The physicist
11th December 2010, 13:50
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:


#include "brachygui.h"
#include "ui_brachygui.h"
#include <QLabel>

BrachyGUI::BrachyGUI(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::BrachyGUI)
{
ui->setupUi(this);
}

BrachyGUI::~BrachyGUI()
{
delete ui;
}

void BrachyGUI::on_pushButton_clicked()
{
label->setText("Monday");
}

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.

tbscope
11th December 2010, 15:24
If you use designer, then the objects you've added via designer are available via the "ui" object. Example:


ui->label->setText("Monday");

The physicist
11th December 2010, 17:11
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).

The physicist
12th December 2010, 15:38
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


void gui_brachy::on_pushButton_clicked()
{
function();

ui->label->setText(variable);
}

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?

squidge
12th December 2010, 15:47
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)";

The physicist
12th December 2010, 18:23
Classes aren't something I've used in my limited experience of C++, so I will go away and read up them now, cheers :D

squidge
12th December 2010, 19:24
Good idea, you'll be using them a lot in Qt programming :)

The physicist
12th December 2010, 20:58
Just cracked it, thanks. I didn't realise that what I put in setText() has to be of type QString!

The physicist
13th December 2010, 13:56
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 :confused:

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.

stampede
13th December 2010, 14:40
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)

The physicist
13th December 2010, 15:25
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 :confused:

high_flyer
13th December 2010, 15:33
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.

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

tbscope
13th December 2010, 16:14
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.

The physicist
13th December 2010, 18:43
Always look at the compile output pane (if you use Qt Creator)This is what prompted me to try switching, sound advice!

The physicist
15th December 2010, 20:46
I'm aware that I am like a boomerang :D

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
QImage logo("logo.jpg"), 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.

tbscope
16th December 2010, 05:37
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.

The physicist
16th December 2010, 21:27
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
private:
Ui::gui_brachy *ui;
QPixmap m_pixmap;
QPainter m_painter;
then add the variables (+a file) like below, and add the line below the setupUi line
gui_brachy::gui_brachy(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::gui_brachy),
m_pixmap("logo.jpg"),
m_painter(&m_pixmap)
{
ui->setupUi(this);
ui->label->setPixmap(m_pixmap);

}

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?

The physicist
17th December 2010, 16:26
Bump - does anyone know about an autoscale function and why the label position gets moved?

agent-x9119
19th December 2010, 00:44
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

squidge
19th December 2010, 10:37
Use an array. Something like:



QButton *myButtons[16];
myButtons[0] = ui->p1;
myButtons[1] = ui->p2;
...


(there are other more efficient ways to initialise the array depending on the scope of the array, but this will work regardless)

then you can do:



for (int i = 0; i < 16; i ++)
{
myButtons[i]->setText("something");
}

marcvanriet
19th December 2010, 22:50
From the documentation : this example returns a child QPushButton of parentWidget named "button1":


QPushButton *button = parentWidget->findChild<QPushButton *>("button1");

You can use QString("p%1").arg(i) to create the strings "p1", "p2", "p3", ...

Regards,
Marc