简体   繁体   中英

How to make QLineEdit editing enabled while text comes from a QPushButton?

In my project, I am using two QPushButton and two QLineEdit . I am connecting these QPushButton with these QLineEdit in such a way, so that QPushButton allow the user to select a folder from hard drive and after selection, the corresponding QLineEdit will display the URL path of the selected folder.

I also like to allow the user to write the URL by himself own if he does not want to click QPushButton and choose folder. And also if the user wants, he can also edit the URL after selecting by QPushButton .

Here I am facing two problems.

1) One QLineEdit allows user to write but another one does not.

2) When user presses on QPushButton , writing mode on corresponding QLineEdit becomes disabled.

The following is the code. Here InputLine and OutputLine are two QLineEdit

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

/* Setting the size of Mainwindow */
this->setWindowTitle("Crop Multiple Object");
this->setFixedHeight(600);
this->setFixedWidth(800);

/* Setting QLabel for displaying Image  */
QLabel* image= new QLabel(this);
image->setGeometry(20,130,500,430);
image->setStyleSheet("QLabel {background-color: rgb(200,200,200)}");
image->show();

/* Set input URL */
QPushButton* InputURL = new QPushButton(this);
InputURL->setText("Input URL");
InputURL->setGeometry(20,30,100,30);
connect(InputURL, SIGNAL(clicked(bool)), this, SLOT(ReceiveInputURL()));

/* Set output URL */
QPushButton* OutputURL = new QPushButton(this);
OutputURL->setText("Output URL");
OutputURL->setGeometry(20,80,100,30);
connect(OutputURL, SIGNAL(clicked(bool)), this, SLOT(ReceiveOutputURL()));

/* Set Input URL Line*/
InputLine->setGeometry(140,30,400,30);
OutputLine->setGeometry(140,80,400,30);
}

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

void MainWindow::ReceiveInputURL()
{
    QFileDialog dialog(this);
    dialog.setNameFilter(tr("Images (*.png *.xpm *.jpg)"));
    dialog.setViewMode(QFileDialog::Detail);
    QString dir = QFileDialog::getExistingDirectory(this, tr("Input Image File"),
                                                 "/home",
                                                 QFileDialog::ShowDirsOnly
                                                 | QFileDialog::DontResolveSymlinks);
    if(!dir.isEmpty())
    {
        InputLine->setText(dir + "/");
    }

}

void MainWindow::ReceiveOutputURL()
{
    QFileDialog dialog(this);
    dialog.setViewMode(QFileDialog::Detail);
    QString dir = QFileDialog::getExistingDirectory(this, tr("Output Image File"),
                                                 "/home",
                                                 QFileDialog::ShowDirsOnly
                                                 | QFileDialog::DontResolveSymlinks);
    if(!dir.isEmpty())
    {
        OutputLine->setText(dir+ "/");
    }
}

I appreciate any help. Thanks in advance.

The problem is that you are creating your QLineEdit objects before the centralWidget of MainWindow is created. This puts the central widget on top of your QLineEdit widgets, so it blocks the mouse events from passing through. To test this, you can disable mouse events for central widget with centralWidget()->setAttribute(Qt::WA_TransparentForMouseEvents); , and you will notice that your QLineEdit widgets can be accessed by mouse clicks.

However, you shouldn't place any widgets directly on the MainWindow . This is not how QMainWindow is supposed to be used. Instead you should place your widgets on the centralWidget . You should read the docs of QMainWindow to know more.

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