PDA

View Full Version : Changing text of button in no relation to button



Sabre Runner
19th September 2010, 11:32
How do I change the text of a PushButton with an action that has no relation to what the button does?
Basically, I want the button to be pressed, some routines to run and if one of them does something right then the text on the button should change.

I thought that just button.setText(qstring) will do the trick but it doesn't. I've tried following it up with update, repaint, paintEvent and a million other things I've found with Google searches but it doesn't work. The only thing I've thought of but haven't tried was to destroy and reload the entire UI.

I could really use some help on this. Thanks.

tbscope
19th September 2010, 11:48
Always try to use the lowest common denominator.
All objects of the main window are accessible via the main window, thus change the text of a component of the main window inside a function or slot of the main window.

Example


void MainWindow::slotButtonClicked()
{
int result = doSomething();

if (result == 1)
button->setText("New text");
}

int MainWindow::doSomething()
{
return something;
}

Sabre Runner
19th September 2010, 13:37
But what if I need the ability to change the text of the button in a function that is triggered from a different button?

Zlatomir
19th September 2010, 13:40
You connect the second button's clicked() signal, with the slot that update the text of your first button.
Read about signals and slots (http://doc.trolltech.com/4.6/signalsandslots.html)

Sabre Runner
19th September 2010, 13:59
I'm in a hurry to get some things done, stress from the bosses and everything, I can't delve too deeply into the mechanism right now.
Why can't button.setText(text) just work as it is?

tbscope
19th September 2010, 14:24
It can.

Against the rules, but here's an easy answer to your homework.


class MainWindow : public something
{
Q_OBJECT

public slots:
void button1Clicked();
void button2Clicked();

private
QPushButton *button1;
QPushButton *button2;
};

MainWindow::MainWindow(...) : ...
{
button1 = new ...;
...

connect(button1, SIGNAL(clicked()), this, slot(button1Clicked()));
...
}

void MainWindow::button1Clicked()
{
button2->setText("Hello!");
}
...


The real question, for me, is: what are you doing coding programs at a company if you don't know basic object oriented programming?

Lykurg
19th September 2010, 14:39
Why can't button.setText(text) just work as it is?Please, show the code, which isn't working for you.

Sabre Runner
19th September 2010, 15:01
I'm a CS student and this is my first real job programming. Thanks for the vote of confidence.

I need the ability to click a tool button, save the path taken so that other routines can use it.


void SubNyquist::on_pushFindIC_clicked()
{
QString path = QDir::currentPath() += tr("/ic1.rbf");
ICpath = QFileDialog::getOpenFileName(this,tr("Open IC File..."),
path,tr("IC Files (*.rbf)"));
ICpath.replace(tr("/"),tr("\\"));
}
And when a certain function, triggered by a different button, finishes correctly, they want the path displayed on the button itself.


void SubNyquist::on_pushLoadIC_clicked()
{
...
doSomething(Regarding the file given)
...
// And if it doesn't break in the middle...
pushFindIC.setText(ICpath);
...
}

Lykurg
19th September 2010, 15:14
ICpath.replace(tr("/"),tr("\\"));
If you are a CS student, then here is the first lesson: Read the documentation! (QDir::toNativeSeparators)

And are you sure pushFindIC is the right object? Because of your pices of code, I am not sure...

Sabre Runner
19th September 2010, 15:20
I do read the documentation but only the parts I think I need. I don't have the time to read the manual start to finish. Not right now.
And how will that work as the path given to the file dialog?

I have pushFindIC which is the Open dialog for the file to load. pushLoadIC is the button to actually load the file and do all the other things which it requires. If that doesn't break, for any reason, that is when I need to open button to change.

Lykurg
19th September 2010, 15:33
And how will that work as the path given to the file dialog?It is static and takes a QString as argument?


I have pushFindIC which is the Open dialog for the file to load. pushLoadIC is the button to actually load the file and do all the other things which it requires.:confused: Isn't pushFindIC your button? So where is the button where you want to change the text? And how do you call it? Are you using an ui file? (Seems so)

wysota
20th September 2010, 00:29
I don't have the time to read the manual start to finish. Not right now.
Saying such things will not get you going on this forum. Unless of course you want to hear that someone has no time to help you right now...

You should really read about signals and slots. Learning some encapsulation and building object oriented modules would be very helpful too.

Sabre Runner
22nd September 2010, 07:54
I'll do that. I will get that first on my list. I would still appreciate any help, if and when you have time.

I am using a ui file. pushFindIC is the button for the open file dialog and the one I want to display the path in the text. pushLoadIC is the one which handles the processing of the file selected and if that process works, the path text should change.

wysota
22nd September 2010, 09:10
What exactly do you have problems with? Which part of your task you don't know how to do? You set button labels with manipulating the QAbstractButton::text property.

Sabre Runner
22nd September 2010, 10:09
Yes. I did that. As far as I can tell, the text is there, saved in the button's property, it just won't display in the GUI on the button itself.

My process, and I probably have no say in how this works, is that button 1 is pressed which opens the dialog to find the file. With file selected, button 2 is ready to go. Button 2 does some other things then executes a function. If that function returns correctly, then button 1's text should update to display the text it has.

Everything seems to work up to that last part of updating or notifying button 1 that it needs to update.

I'm reading the Signals and Slots tutorial now and trying to implement what I learn but so far no dice.

Lykurg
22nd September 2010, 10:16
I guess at this stage we only can help you, if you provide a minimal compilable example reproducing your problem.

Sabre Runner
22nd September 2010, 10:32
void Window::on_pushButton1_clicked()
{
path = QFileDialog::getOpenFileName(this, tr("Open File..."), path,tr("Files (*.*)"));
path = QDir::toNativeSeparators(path);
pushButton1.setText(path);
}

This is the button that preps the path to be used to load the file from.


void Window::on_pushButton2_clicked()
{
try something...
catch one exception...
{
output error 1...
}
catch second exception...
{
output error 2...
}
catch all other exceptions...
{
output generic error...
}
QMessageBox::information(this,tr("Loaded"),tr("Loaded Successfully"));
perform some other operations and configurations for the future...
}


What I'd like to do is to change the text of button 1 if and only if button 2's operation has reached the loaded successfully part.

Lykurg
22nd September 2010, 10:43
I was saying: compilable. The error must be somewhere in your design.

Sabre Runner
22nd September 2010, 10:47
Am I forgetting to connect something? Is something unavailable? Is it because I'm using the UI designer? Are there several places were where signals and slots must be defined?

Because I tried defining it with just a button release (hoping it will catch on one the button's operating would be done) signal to a set text slot in the designer and in the application start up and it hasn't worked. I can't seem to change anything in the UI from with in the code itself.

wysota
22nd September 2010, 10:55
Can you resize windows, click buttons and do all other stuff with your GUI after the button's text property is changed (and changes are not reflected on the button itself)? Maybe you are simply blocking the event loop?

ChrisW67
23rd September 2010, 07:04
It appears you want the user to push button 1 to select a file. You then change the label on button 1 to the path the user selected. You then expect the user to push button 2 to process the file selected when they pushed button 1.

Quoting out of order:


What I'd like to do is to change the text of button 1 if and only if button 2's operation has reached the loaded successfully part.

Then why do you set it in the handler for the pushButton1 clicked signal? Line 5 in this chunk:



void Window::on_pushButton1_clicked()
{
path = QFileDialog::getOpenFileName(this, tr("Open File..."), path,tr("Files (*.*)"));
path = QDir::toNativeSeparators(path);
pushButton1.setText(path);
}

This is the button that preps the path to be used to load the file from.

should probably be after line 16 of this chunk where the processing has been successful after the user presses button 2:




void Window::on_pushButton2_clicked()
{
try something...
catch one exception...
{
output error 1...
}
catch second exception...
{
output error 2...
}
catch all other exceptions...
{
output generic error...
}
QMessageBox::information(this,tr("Loaded"),tr("Loaded Successfully"));
perform some other operations and configurations for the future...
}



You also need to ensure that button 2 cannot be pressed unless button 1 has been AND the user did not cancel the file selection. Something has to reset button 1's label too I guess.

SixDegrees
23rd September 2010, 07:40
I hate to keep suggesting "printf-style" debugging, but here's another case where it would probably shed light on what's going on. Placing output statements in your code just prior to your call to button.setText() would allow you to see if that portion of your code is getting called in the first place, and if so to dump the value of local variables that may be related to your problems.

The preferred method for determining such things, of course, is to run in a debugger, but for simple problems it's often best to use simple solutions.

Also, I should point out that trying to display paths on buttons is a remarkably poor UI decision. As soon as you hit a case where the path is too long to show on the button, and you realize that you can't scroll to see the other end of it, the design breaks and breaks badly. And that's probably going to happen within minutes of actual use.

Part of your job as a software engineer will be to bring such matters to a client's attention before they cause such problems, and to provide reasonable alternative solutions.

Sabre Runner
23rd September 2010, 12:29
Yes. I even added that same set text command at the start of the program but it doesn't change the button.
Do I need to update or repaint? And if so, how? Because I tried those some times and they didn't work either.