Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QComboBox LineEdit crashing



  • Hello,
    I'm trying to set the alignment in my QComboBox line edit field.

    #include <QComboBox>
    #include <QLineEdit>
    ui->cmbTest->lineEdit()->setAlignment(Qt::AlignHCenter);
    

    I found out that the function:

    ui->cmbTest->lineEdit();
    

    Was actually returning a null pointer. That is when I do this:

    QLineEdit *tmp = ui->cmbTest->lineEdit();
    

    tmp = NULL

    What am I doing wrong?

    Thank you.



  • @Reghu_Att
    Read the docs! :)

    https://doc.qt.io/qt-5/qcombobox.html#lineEdit

    Returns the line edit used to edit items in the combobox, or nullptr if there is no line edit.

    Only editable combo boxes have a line edit.

    https://doc.qt.io/qt-5/qcombobox.html#editable-prop

    This property holds whether the combo box can be edited by the user

    By default, this property is false. The effect of editing depends on the insert policy.

    (So, to be clear, if you have not gone ui->cmbTest->setEditable(true) you won't have a line edit!)



  • @jonb

    Hello. Thanks for the response.
    I ended up making the combo boxes editable in QtCreator and then doing this:

    ui->cmbTest->lineEdit()->setReadOnly(true);
    ui->cmbTest->lineEdit()->setAlignment(Qt::AlignHCenter);
    

    Regards.


Log in to reply