5

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?

scopchanov
  • 7,966
  • 10
  • 40
  • 68
annie
  • 143
  • 1
  • 1
  • 11
  • 2
    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. – eyllanesc Aug 09 '17 at 18:04
  • Try `QLineEdit *q = new QLineEdit(this);` – scopchanov Aug 09 '17 at 23:05
  • What @scopchanov will work if `this` is a view. – user2836202 Aug 09 '17 at 23:10
  • Sorry, but I can't understand what you mean. – scopchanov Aug 09 '17 at 23:12
  • It seems correct, can you possibly post the code of this complete class/module? – PRIME Aug 10 '17 at 05:32
  • 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 set – annie Aug 10 '17 at 06:19
  • Is there any connection between QLineEdit *q and the object name of my lineEdit? – annie Aug 10 '17 at 06:29

1 Answers1

6

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);
}
scopchanov
  • 7,966
  • 10
  • 40
  • 68