PDA

View Full Version : an Unchekcable push Button is checked and stays checked when used with qfiledialog



artome
21st February 2019, 11:54
Hi I have strange problem.
I have a widget gui with a not checkable button that when pressed opens a file dialog.

here is the signal

connect(ui->loadcsv,SIGNAL(pressed()),this, SLOT(fromcsv()));

and the start of the slot

void newinstrument::fromcsv(){
QString line,filename;
QStringList filesnames;

QFileDialog dialog(this);
dialog.setFileMode(QFileDialog::ExistingFile);
dialog.setDirectory(folder);
if(dialog.exec())filesnames=dialog.selectedFiles() ;

.....

This always leaves the loadcsv button as checked.

although this being a noncheckable button I still tried to explicitly put the status of the button as unchecked before returning the fromscv slot but the result do not change. After being pressed once the button stays always checked but other from that everything seem to work fine (I called this checked due only to the visual effect but really is not a checked status as a second click does not put the button with the visual aspect of an unchecked button).

any hints would be welcome

P.s.
I'm using qt12.1 from opensuse leap 15.0

anda_skoa
21st February 2019, 12:46
Recommendation 1: use the button's clicked() signal
Recommendation 2: use QFileDialog::getOpenFileNames()

Cheers,
_

artome
21st February 2019, 17:53
The clicked signal worked as i always use pressed signal i'll try to understand the difference.
cheers

d_stranz
22nd February 2019, 17:18
The clicked signal worked as i always use pressed signal i'll try to understand the difference.

The difference is that the pressed() signal is emitted as soon as the mouse goes down on the button. The clicked() signal is emitted only when the mouse goes down and then comes back up while on the button. This difference has at least three consequences:

- If you connect to pressed(), then there is no way for the user to cancel the action - whatever is in the slot gets executed as soon as the mouse goes down.
- If the slot connected to pressed() causes the button to lose focus, then the button will never see a mouse release so it stays depressed. This is what happened for you.
- If you connect to clicked() and the user presses the mouse on the button but then moves off the button to release the mouse, there is no signal. This lets the user "cancel" the click before anything happens. The clicked() signal is emitted only if the mouse down and up happen while the mouse is on the button. (The mouse can move off the button while it is pressed, but if it moves back on when it is released, the signal will be emitted).

In general, you should always connect to the clicked() signal for a good user experience.