Changing text of button in no relation to button
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.
Re: Changing text of button in no relation to button
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
Code:
void MainWindow::slotButtonClicked()
{
int result = doSomething();
if (result == 1)
button->setText("New text");
}
int MainWindow::doSomething()
{
return something;
}
Re: Changing text of button in no relation to button
But what if I need the ability to change the text of the button in a function that is triggered from a different button?
Re: Changing text of button in no relation to button
You connect the second button's clicked() signal, with the slot that update the text of your first button.
Read about signals and slots
Re: Changing text of button in no relation to button
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?
Re: Changing text of button in no relation to button
It can.
Against the rules, but here's an easy answer to your homework.
Code:
class MainWindow : public something
{
Q_OBJECT
public slots:
void button1Clicked();
void button2Clicked();
private
};
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?
Re: Changing text of button in no relation to button
Quote:
Originally Posted by
Sabre Runner
Why can't button.setText(text) just work as it is?
Please, show the code, which isn't working for you.
Re: Changing text of button in no relation to button
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.
Code:
void SubNyquist::on_pushFindIC_clicked()
{
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.
Code:
void SubNyquist::on_pushLoadIC_clicked()
{
...
doSomething(Regarding the file given)
...
// And if it doesn't break in the middle...
pushFindIC.setText(ICpath);
...
}
Re: Changing text of button in no relation to button
Quote:
Originally Posted by
Sabre Runner
Code:
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...
Re: Changing text of button in no relation to button
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.
Re: Changing text of button in no relation to button
Quote:
Originally Posted by
Sabre Runner
And how will that work as the path given to the file dialog?
It is static and takes a QString as argument?
Quote:
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)
Re: Changing text of button in no relation to button
Quote:
Originally Posted by
Sabre Runner
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.
Re: Changing text of button in no relation to button
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.
Re: Changing text of button in no relation to button
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.
Re: Changing text of button in no relation to button
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.
Re: Changing text of button in no relation to button
I guess at this stage we only can help you, if you provide a minimal compilable example reproducing your problem.
Re: Changing text of button in no relation to button
Code:
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.
Code:
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.
Re: Changing text of button in no relation to button
I was saying: compilable. The error must be somewhere in your design.
Re: Changing text of button in no relation to button
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.
Re: Changing text of button in no relation to button
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?