How to show hidden item
-
I am trying to do the following:
- Press button
- progress bar pops up, showing us the battery status
To start with, I am trying to jast have the progress bar show at the click of a button.
After I drew my button and progress bar in the form, I used signals and slot to do the following:Sender Signal Receiver Slot
pushButton toggled(bool) progressBar setVisible(bool)in my mainwindows.cpp I have the following:
ui->progressBar->hide();I am assuming that when I click the button, the progress bar should pop up, otherwise
it should stay hidden. This is not happening. What am I missing?Thanks
-
The bool in toggled(bool) is for QPushButton "checked" state as in if the button is checkable.
from what you say all you need is a custom slot with a signal leading to it from the button like:
@
connect(pushButton, SIGNAL(clicked()), this, SLOT(ButtonClicked()));void YourClass::ButtonClicked() {
if (ui->progressBar->isVisible())
ui->progressBar->setVisible(false);
else
ui->progressBar->setVisible(true);
}
@If you want the progressBar however to only be visible while "holding down your mouse over the button" you can over-ride the mousePress and mouseRelease events to do the same.
Or
if you set the button as checkable you would then be able to use the toggled signal to get the state and hence set the progressBar visibility from it.
-
Hi, thanks for the tip. I am a complete novice in C++, so I think I am running into some problems. Here is my code,including what you suggested (the connect statement is in a different file):
@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGui>
#include "digitalclock.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->progressBar->hide();
}void MainWindow::ButtonClicked() {
if (ui->progressBar->isVisible())
ui->progressBar->setVisible(false);
else
ui->progressBar->setVisible(true);
}MainWindow::~MainWindow()
{
delete ui;
}
@This tells me that the following:
C:\Users\menu-build-desktop-Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug..\menu\mainwindow.cpp:14: error: no 'void MainWindow::ButtonClicked()' member function declared in class 'MainWindow'
What do I need to do to fix this?
Thanks
[Edit: Added @ code tags; mlong]
-
Hi,
Kindly use code formatter for your code while posting or use @ .
For the above example u need to add a slot in mainwindow.heg:
@class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private slots:
void ButtonClicked();private:
Ui::MainWindow *ui;
};@or else if you have the pushButton in your designer just Right Click -- Go to Slot-- Clicked(bool)
and then write the code for hiding or showing the component. -
Appreciarte the help. I appologize, I am new to all this hence unfamiliar with most of what you are talking about. Is formatter a tool I can use?
I got what I wanted to do in a different way. In the Mainwindows.ui file (in graphical view), I connected the button to the progress bar so that while it is pressed the status bar shows up and when unpressed the status bar goes away. So I used two connect signals and slots statements.
Thanks for the tips guys, I appreciate it.
-
Now I need to figure out how to show actual battery status in the progress bar. I will try to use the example they have in the QT examples folder. I will be back if things dont go well.
-
formatter is just the button you find when posting something on the forums that looks like a document with "<>" on it. It's at the top right while your creating your message.
Simple solution is if you want to show a peice of code use @ before and after the code so it appears in a better to read way :)
-
@Ah thank you :)@
-
lol dont use it for everything though. It's just for code hence why they're "Code tags" :)