Placeholder in qt
Asked Answered
E

1

6

I want to set a placeholder text in a QLineEdit. I am using the below code to do so:

QLineEdit *q = new QLineEdit;
q->setPlaceholderText("Enter number");

But on executing, the placeholder does not set. What may be the problem here?

Evonevonne answered 9/8, 2017 at 17:21 Comment(7)
If you put text with setText(), the placeholder will disappear, you are sure that you have not done that. With the code you show it is impossible to solve the problem. It would be great to share your complete code through github, drive, etc to review it.Icarian
Try QLineEdit *q = new QLineEdit(this);Kappenne
What @Kappenne will work if this is a view.Bittner
Sorry, but I can't understand what you mean.Kappenne
It seems correct, can you possibly post the code of this complete class/module?Indult
There is no connection of this with my program as of now. I have a lineedit into which i want the user to enter the number of items required. To indicate this, I want to set the placeholder text into the linedit using the above two lines of code, but on running, the lineedit is blank without any placeholder setEvonevonne
Is there any connection between QLineEdit *q and the object name of my lineEdit?Evonevonne
K
7

As the isolated code you have provided is not enough to give us a clue where the problem is, I suggest you to try this minimalistic example, see if it works for you and adapt it for your purpose. If the adaptation does not work, then post the changes you have made to discuss them.

MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLineEdit>

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = nullptr);
};

#endif // MAINWINDOW_H

MainWindow.cpp

#include "MainWindow.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
    QLineEdit *q = new QLineEdit(this);
    q->setPlaceholderText("Enter number");
    setCentralWidget(q);
}
Kappenne answered 10/8, 2017 at 10:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.