Sanakum
Sanakum

Reputation: 179

How to set a QSpinBox when QLabel get a QImage in Qt?

I have QSpinBox in my QWidget which I want to set only when a QLabel get an QImage. Are there any function or tool by which I can set a QSpinBox at any condition?

Here, how I have worked is given below

At first, I declare a QSpinBox object and set it's maximum

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

I have a QPushButton by pressing which, user can load an image, which will be displayed in QLabel.

void MainWindow::on_Browse_clicked()
{
    QFileDialog dialog(this);
    dialog.setNameFilter(tr("Images (*.png *.xpm *.jpg)"));
    dialog.setViewMode(QFileDialog::Detail);
    QString imagefileName = QFileDialog::getOpenFileName(this, tr("Open File"), "Given_Path", tr("Images (*.png *.xpm *.jpg)"));

    if(!imagefileName.isEmpty())
    {
        image= QImage(imagefileName);
        ui->label->setPixmap(QPixmap::fromImage(image));  

        spinbox= new QSpinBox(this); 
        QPoint p(100,300);
        spinbox->move(p);     
    }   
}

But it is not displaying any QSpinBox how I tried to get.

I would appreciate your any kind of help.

Upvotes: 2

Views: 236

Answers (1)

Gansroy
Gansroy

Reputation: 229

You can try QSpinBox::setEnabled(bool) function, where QSpinBox::setEnabled(true) will enable your SpinBox and similarly QSpinBox::setEnabled(false) will disable this.

I think you can better declare QSpinBox inside MainWindow function and put it disabled. So you can try this.

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    spinbox= new QSpinBox(this);
    QPoint p(100,300);
    spinbox->move(p);   

    spinbox->setEnabled(false);    //here disabled QSpinBox
}

And inside Browse clicked function, you can put in enabled.

void MainWindow::on_Browse_clicked()
{
    QFileDialog dialog(this);
    dialog.setNameFilter(tr("Images (*.png *.xpm *.jpg)"));
    dialog.setViewMode(QFileDialog::Detail);
    QString imagefileName = QFileDialog::getOpenFileName(this, tr("Open File"), "Given_Path", tr("Images (*.png *.xpm *.jpg)"));

    if(!imagefileName.isEmpty())
    {
        image= QImage(imagefileName);
        ui->label->setPixmap(QPixmap::fromImage(image));       

        spinbox->setEnabled(true);  // Here enabled QSpinBox
    }    
}

Hope it will help you.

Upvotes: 1

Related Questions