简体   繁体   English

QT无法使用自动调整大小的QLineEdit和布局

[英]QT Unable to use an autosize QLineEdit & Layout

I have created a custom widget extending QLineEdit. 我创建了一个自定义窗口小部件,扩展了QLineEdit。 I want this is 'autosize'. 我希望这是“自动调整大小”。 Ok I get it using 好吧,我用

connect(this,SIGNAL(textChanged(QString)), this, SLOT(resizeLineEditToContents()));

I have 2 of this widgets. 我有两个这样的小部件。 The idea is that a resize of widget 1 is used by the horizontal layout where I have placed the two widgets to place rightly the widget 2. 这个想法是调整小部件1的大小由水平布局使用,在该布局中我放置了两个小部件以正确放置小部件2。

But it is impossible, I have played with all the policy possible value, without success. 但是这是不可能的,我一直在尝试所有可能的政策价值,但没有成功。

The layout tries always to adjust the size of my QLineEdit widget .... (It happens also with a standard QLineEdit ). 布局总是尝试调整我的QLineEdit小部件的大小...。(标准QLineEdit也会发生这种情况)。 Any help? 有什么帮助吗?
Thanks 谢谢

Actually, it's not necessary to have the signature of signals and slots to be the same, and Qt actually asks in the documentation to not put 'const' and '&' on it because of the 'normalization'. 实际上,没有必要使信号和插槽的签名相同,并且Qt实际上在文档中要求不要由于“规范化”而在其上放置“ const”和“&”。 a Signal can have zero to N parameters, a slot can bave zero to Signal-Number-Of-Parameters, so that's why the connect works. 一个Signal可以有零到N个参数,一个插槽可以给Signal-Number-Of-Parameters零,所以这就是连接工作的原因。

but back to your problem, you need to use setFixedWidth, so it will know to not resize back to the old width just as soon as you stretch it, then set Layout to prefered, so it will respect your widget's hints. 但是回到您的问题,您需要使用setFixedWidth,因此它将知道不要在拉伸后立即将其调整为旧宽度,然后将Layout设置为Preferred,这样它将尊重您的小部件提示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM