如何在QLineEdit中放置静态文本(后缀,前缀)? [英] How to put a static text (postfix, prefix) in QLineEdit?

查看:51
本文介绍了如何在QLineEdit中放置静态文本(后缀,前缀)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Qt C ++中的 QLineEdit 中放置静态文本这样就不能删除它,并且当我写QLineEdit时,它不应该隔开.

解决方案

没有常规方法在 QLabel 中添加前缀或后缀.

占位符

据您所知,使用 QLineEdit 可以设置一个文本,当没有文本指示时将显示该文本-请参见 QLineEdit :: placeholderText .

InputMask

使用 QLineEdit 的另一种方法是设置 inputMask ,但是它将更改光标,并且需要特定数量的字母.

带有QLabel的后缀

如果您知道最大数量的符号并想做一个后缀,则可以使用另一个 QLabel :
来获得它.1.将文本的 length 限制为在右侧具有一定的可用空间.
2.在 QLineEdit 的右侧放置 QLabel ,然后在其中输入一个后缀文本.

注意:如果 QLineEdit 在布局内,您将无法将 QLabel 放在 QtDesigner 中的 QLineEdit 上.在这种情况下,您可以在布局中添加 QWidget 而不是 QLineEdit ,然后将 QLineEdit QLabel 放在此窗口小部件中,没有布局.您也可以在代码中创建 QLabel :

  QLabel *标签=新的QLabel(this);label-> setText("kg");label-> move(lineEdit-> rect().right()-label-> width()-3,lineEdit-> rect().center().y()-label-> height()/2); 

自定义小部件

添加后缀和前缀的最灵活的方法是创建一个继承自 QWidget 的新类,添加两个 QLabels (前缀和后缀)信息,并添加 QLineEdit 并使用CSS使它们看起来像单个 QLineEdit .

在此图像上:前缀和后缀是 QLabels ._some_text_是名为 lineEdit QLineEdit ,它们全部位于水平布局中的名为 complexLineEdit QWidget 中.

这是我用于上图的css:

  QWidget#complexLineEdit{border-top:1px实心#CCCCCC;左边框:1px 实心 #DDDDDD;右边框:1px实心#DDDDDD;border-bottom:1px实线#DDDDDD;背景颜色:白色;}QWidget#complexLineEdit QLineEdit#lineEdit{边框:0px;} 

您可以使用它来使其更类似于 QLineEdit .

How can I put a static text in QLineEdit in Qt C++ so that it can not be deleted and when I write to QLineEdit, it should not be spaced.

解决方案

There is no regular way to put a prefix or postfix in QLabel.

Placeholder

As far as you can get with QLineEdit is to set a text which will be displayed when there is no text indide - see QLineEdit::placeholderText.

InputMask

Another way to do it with QLineEdit is to set inputMask but it will change a cursor and will require a specific amount of letters.

Postfix with QLabel

If you know maximum amount of symbols and want to make a postfix, you can get it with another QLabel:
1. Limit length of the text to have a certain free space at right.
2. Place QLabel to the right side of the QLineEdit and enter a postfix text into it.

NOTE: you won't be able to put QLabel on QLineEdit in QtDesigner if the QLineEdit is inside of a layout. In this case you can add QWidget instead of QLineEdit in the layout and put QLineEdit and QLabel within this widget which doesn't have a layout. Also you can create QLabel in the code:

QLabel* label = new QLabel(this);
label->setText("kg");
label->move(lineEdit->rect().right() - label->width() - 3, lineEdit->rect().center().y() - label->height() / 2);

Custom Widget

Most flexible way to add a postfix and a prefix is to create a new class inherited from QWidget add two QLabels (prefix and postfix) info it, add QLineEdit between them and use css to make them look like a single QLineEdit.

On this image: Prefix and postfix are QLabels. _some_text_ is QLineEdit named lineEdit and all of them are inside a QWidget named complexLineEdit in a horizontal layout.

Here is a css I used for the image above:

QWidget#complexLineEdit
{
  border-top: 1px solid #CCCCCC;
  border-left: 1px solid #DDDDDD;
  border-right: 1px solid #DDDDDD;
  border-bottom: 1px solid #DDDDDD; 
  background-color: white;
}

QWidget#complexLineEdit QLineEdit#lineEdit
{
   border: 0px;
}

You can play with it to make it even more similar to QLineEdit.

这篇关于如何在QLineEdit中放置静态文本(后缀,前缀)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆