简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM