简体   繁体   English

QLabel在Qt中获取QImage时如何设置QSpinBox?

[英]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 .我的QWidgetQSpinBox ,我只想在QLabel获得QImage时设置它。 Are there any function or tool by which I can set a QSpinBox at any condition?是否有任何功能或工具可以在任何条件下设置QSpinBox

Here, how I have worked is given below在这里,我的工作方式如下

At first, I declare a QSpinBox object and set it's maximum首先,我声明了一个QSpinBox对象并将其设置为最大值

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 .我有一个QPushButton按下它,用户可以加载一个图像,该图像将显示在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.但它没有显示我试图获得的任何QSpinBox

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.您可以尝试QSpinBox::setEnabled(bool)函数,其中QSpinBox::setEnabled(true)将启用您的SpinBox ,类似地QSpinBox::setEnabled(false)将禁用此功能。

I think you can better declare QSpinBox inside MainWindow function and put it disabled.我认为您可以更好地在MainWindow函数中声明QSpinBox并将其禁用。 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.希望它会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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