Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with QLineEdit and returnPressedSignal
Qt 6.11 is out! See what's new in the release blog

Problem with QLineEdit and returnPressedSignal

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by A Former User
    #1

    Hi! I would like to share with you a problem that I am having. To simplify I will just paste the code of a test app to show my problem.

    This is a QLineEdit that handles a returnPressed signal calling the SLOT "click()" of a button. When pressed, click() is activated and onButtonClicked is called. Then the UI is deleted and reloaded with a new QLineEdit. Finally, after loading everything it crashes.

    But if I DO NOT PRESS RETURN BUTTON and just click with the mouse on the click button, onButtonClicked is also called and nothing crashes. Elements are deleted and the UI is reloaded correctly.

    I am a little bit lost looking for the solution of my problem.

    CODE:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QLineEdit>

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

    connect(ui->pushButton_next, SIGNAL(clicked(bool)), this, SLOT(onButtonClicked()));
    run();
    

    }

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

    void MainWindow::run()
    {
    QHBoxLayout* hLayout = new QHBoxLayout();
    ui->verticalLayout->addLayout(hLayout);

    QLineEdit* lineEdit = new QLineEdit();
    hLayout->addWidget(lineEdit);
    
    connect(lineEdit, SIGNAL(returnPressed()), ui->pushButton_next, SLOT(click()));
    

    }

    void MainWindow::onButtonClicked()
    {
    QHBoxLayout hLayout = (QHBoxLayout) ui->verticalLayout->itemAt(0);
    QLayoutItem* item = hLayout->takeAt(0);
    QLineEdit lineEdit = (QLineEdit) item->widget();

    lineEdit->disconnect();
    delete lineEdit;
    delete item;
    delete hLayout;
    
    run();
    

    }

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      I answer to myself: use deleteLater() in widgets and layouts.

      Thank's anyway and hope it can help others ;)

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved