Signal released() not working



  • Hi everyone,
    I have written a code in which I have put an image on button. Now I want when I press the button, the image shall be replaced by another image which highlights that the button is pressed. And as soon as the button is released it shall be replace by the previous image.
    In my code The image is getting replaced as soon as i press it but on releasing there is no change, it means that just 1 image gets replaced on button.
    Please can anyone tell me why released() signal is not working?

    CODE: (.cpp file)

    @

    #include "overview.h"
    #include "ui_overview.h"
    #include <QFile>
    #include <QMessageBox>
    #include <QIcon>

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

    *QObject::connect(ui->myButton3, SIGNAL(pressed()),this, SLOT(on_myButton3_clicked()));
    QObject::connect(ui->myButton3, SIGNAL(released()),this, SLOT(on_myButton3_released()));

    QString fileName("D:/solar4000/QT/images/memory.png");
    QString fileName1("D:/solar4000/QT/images/actual.png");
    QString fileName2("D:/solar4000/QT/images/power.png");

    if(QFile(fileName).exists() == false)
    QMessageBox::warning(this, "Application", "Wrong file name : " + fileName);
    else

    ui->myButton->setIcon(QIcon(fileName));
    

    if(QFile(fileName1).exists() == false)
    QMessageBox::warning(this, "Application", "Wrong file name : " + fileName);
    else
    ui->myButton2->setIcon(QIcon(fileName1));

    if(QFile(fileName2).exists() == false)
    QMessageBox::warning(this, "Application", "Wrong file name : " + fileName);
    else
    ui->myButton3->setIcon(QIcon(fileName2));

    ui->myButton4->setIcon(QIcon("D:/solar4000/QT/images/battery.png"));

    }

    void Overview::on_myButton3_clicked()
    {
    ui->myButton3->setIcon(QIcon("D:/solar4000/QT/images/power-whenclicked.png"));
    }

    void Overview::on_myButton3_released()
    {

    ui->myButton3->setIcon(QIcon("D:/solar4000/QT/images/power.png"));

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

    @

    Thanks



  • The easiest way to do this, is to use style sheets. By the way, you're missing an opening brace on line 36 after the else.

    I think the main problem in your code is, that you are confusing the clicked() and the pressed() signals. A click is a press followed by a release. So the order of the signals is pressed -> released -> clicked. That causes that the image is replaced twice in very quick consession, and you can't see the first change...



  • So in my case where I want to highlight that a button is pressed and released by changing the icon image you are trying to say that this press and release signal won't work?

    By stylesheets do you mean QPixmap and QPainter...?

    Can you please just give me a simple example...I am not able to get it from Qt Documentation.....

    And there is one more doubt if you can please explain...Here in this app you know that I am trying to place an icon on a pushbutton....Now when I keep the size of image and button exactly equal the feel of pressing the button (i mean when i press button it goes down) does not happens. But for now I have just kept the size of image little less than the button, so in result I get a small outline left but the feeling of button being pressed can be seen....I know its a wrong way but can you please tell me what to do....It will be a great help.....looking forward....Thanks andre
    Pragati



  • [quote author="pragati" date="1334309110"]So in my case where I want to highlight that a button is pressed and released by changing the icon image you are trying to say that this press and release signal won't work?[/quote]
    No, it can work, if you actually use the pressed() signal instead of the clicked() signal.
    [quote]
    By stylesheets do you mean QPixmap and QPainter...?
    [/quote]
    No, I mean the "Qt style sheets":/doc/qt-4.7/stylesheet.html mechanism.

    [quote]
    Can you please just give me a simple example...I am not able to get it from Qt Documentation.....
    [/quote]
    Try from the tips above first. If you can't make it work, show what you did, and ask for advice again. We're not doing your homework for you.

    [quote]
    And there is one more doubt if you can please explain...Here in this app you know that I am trying to place an icon on a pushbutton....Now when I keep the size of image and button exactly equal the feel of pressing the button (i mean when i press button it goes down) does not happens. But for now I have just kept the size of image little less than the button, so in result I get a small outline left but the feeling of button being pressed can be seen....I know its a wrong way but can you please tell me what to do....It will be a great help.....looking forward....Thanks andre
    Pragati[/quote]
    I have no idea what you mean by that. Perhaps you can make a couple of screen shots (cropped to the button) and put these in your next message?



  • bq.
    No, it can work, if you actually use the pressed() signal instead of the clicked() signal

    I have used pressed signal in my code and on_clicked is as a slot which is defined below.....so in what other way you want me to use pressed () signal?

    bq.
    Try from the tips above first. If you can't make it work, show what you did, and ask for advice again. We're not doing your homework for you.

    I am not asking you to do homework for me....I am asking advice (as I take you as an expert's guidance) the way to use QPixmap or QPainter for the requirement.....(not just write a code or explain the whole Qt classes with demos nd examples)

    bq. I have no idea what you mean by that. Perhaps you can make a couple of screen shots (cropped to the button) and put these in your next message?

    Images i cannot post it now but will sure in another couple of hours.....



  • Hi,
    You can try the following code for your requirement

    @QString myButton3StyleSheet;
    myButton3StyleSheet += "QPushButton { image: url(D:/solar4000/QT/images/power.png); border: 1px solid black;}";
    myButton3StyleSheet += "QPushButton::pressed { image: url(D:/solar4000/QT/images/power-whenclicked.png);}";
    ui->myButton3->setStyleSheet(myButton3StyleSheet);@

    Instead of directly hard-coding the path you can create a .qrc file and store your images in resource folder. "Creating Resource files":http://www.voidrealms.com/viewtutorial.aspx?id=184


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.