Property contentItem in Qml










0















The following Qml code gives the following output (expected):



import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Window 2.11

Window
height: 200
width: 200
visible: true

Button
id: root
text: "Text"
anchors.centerIn: parent

Item
anchors.fill: parent
Text
text: "Item.Text"
color: "red"






enter image description here



The following code (using contentItem) produces a different output:



import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Window 2.11

Window
height: 200
width: 200
visible: true

Button
id: root
text: "Text"
anchors.centerIn: parent

contentItem: Item
anchors.fill: parent
Text
text: "Item.Text"
color: "red"






enter image description here



The Qt documentation is not very clear, at least for me. The question is what does the property contentItem do? When it should be used?










share|improve this question






















  • the contentItem is a way to customize a control, in your first code you are adding a child item to the button, and in the second customizing the same button, so both implementations have different objectives so both implementations are correct, so you must use one or the other according to what you want, so what output do you want?

    – eyllanesc
    Nov 14 '18 at 19:10











  • @eyllanesc: how did you conclude that contentItem is used to customize a Qml object? Reading the Qt documentation I couldn't conclude that. Does contentItem override the caption only?

    – DJJ
    Nov 14 '18 at 19:14











  • Qt Quick Controls II allows you to customize using that element, check the docs: see doc.qt.io/qt-5.11/qml-qtquick-controls2-control.html

    – eyllanesc
    Nov 14 '18 at 19:16











  • no, actually you should use the control text (button) inside the contentitem, consider that it has the same task as the delegates in the views, for example you can make the button is square, circle, etc., that has an image as a background , etc., that has a color with respect to a state, etc. See doc.qt.io/qt-5.11/…

    – eyllanesc
    Nov 14 '18 at 19:18












  • @eyllanesc: I really don't understand a single word of whatever you are saying. I visited all the links you have shared. Anyway, thanks for the comments.

    – DJJ
    Nov 14 '18 at 19:22















0















The following Qml code gives the following output (expected):



import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Window 2.11

Window
height: 200
width: 200
visible: true

Button
id: root
text: "Text"
anchors.centerIn: parent

Item
anchors.fill: parent
Text
text: "Item.Text"
color: "red"






enter image description here



The following code (using contentItem) produces a different output:



import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Window 2.11

Window
height: 200
width: 200
visible: true

Button
id: root
text: "Text"
anchors.centerIn: parent

contentItem: Item
anchors.fill: parent
Text
text: "Item.Text"
color: "red"






enter image description here



The Qt documentation is not very clear, at least for me. The question is what does the property contentItem do? When it should be used?










share|improve this question






















  • the contentItem is a way to customize a control, in your first code you are adding a child item to the button, and in the second customizing the same button, so both implementations have different objectives so both implementations are correct, so you must use one or the other according to what you want, so what output do you want?

    – eyllanesc
    Nov 14 '18 at 19:10











  • @eyllanesc: how did you conclude that contentItem is used to customize a Qml object? Reading the Qt documentation I couldn't conclude that. Does contentItem override the caption only?

    – DJJ
    Nov 14 '18 at 19:14











  • Qt Quick Controls II allows you to customize using that element, check the docs: see doc.qt.io/qt-5.11/qml-qtquick-controls2-control.html

    – eyllanesc
    Nov 14 '18 at 19:16











  • no, actually you should use the control text (button) inside the contentitem, consider that it has the same task as the delegates in the views, for example you can make the button is square, circle, etc., that has an image as a background , etc., that has a color with respect to a state, etc. See doc.qt.io/qt-5.11/…

    – eyllanesc
    Nov 14 '18 at 19:18












  • @eyllanesc: I really don't understand a single word of whatever you are saying. I visited all the links you have shared. Anyway, thanks for the comments.

    – DJJ
    Nov 14 '18 at 19:22













0












0








0








The following Qml code gives the following output (expected):



import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Window 2.11

Window
height: 200
width: 200
visible: true

Button
id: root
text: "Text"
anchors.centerIn: parent

Item
anchors.fill: parent
Text
text: "Item.Text"
color: "red"






enter image description here



The following code (using contentItem) produces a different output:



import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Window 2.11

Window
height: 200
width: 200
visible: true

Button
id: root
text: "Text"
anchors.centerIn: parent

contentItem: Item
anchors.fill: parent
Text
text: "Item.Text"
color: "red"






enter image description here



The Qt documentation is not very clear, at least for me. The question is what does the property contentItem do? When it should be used?










share|improve this question














The following Qml code gives the following output (expected):



import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Window 2.11

Window
height: 200
width: 200
visible: true

Button
id: root
text: "Text"
anchors.centerIn: parent

Item
anchors.fill: parent
Text
text: "Item.Text"
color: "red"






enter image description here



The following code (using contentItem) produces a different output:



import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Window 2.11

Window
height: 200
width: 200
visible: true

Button
id: root
text: "Text"
anchors.centerIn: parent

contentItem: Item
anchors.fill: parent
Text
text: "Item.Text"
color: "red"






enter image description here



The Qt documentation is not very clear, at least for me. The question is what does the property contentItem do? When it should be used?







qt qml qtquick2






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 19:06









DJJDJJ

5111




5111












  • the contentItem is a way to customize a control, in your first code you are adding a child item to the button, and in the second customizing the same button, so both implementations have different objectives so both implementations are correct, so you must use one or the other according to what you want, so what output do you want?

    – eyllanesc
    Nov 14 '18 at 19:10











  • @eyllanesc: how did you conclude that contentItem is used to customize a Qml object? Reading the Qt documentation I couldn't conclude that. Does contentItem override the caption only?

    – DJJ
    Nov 14 '18 at 19:14











  • Qt Quick Controls II allows you to customize using that element, check the docs: see doc.qt.io/qt-5.11/qml-qtquick-controls2-control.html

    – eyllanesc
    Nov 14 '18 at 19:16











  • no, actually you should use the control text (button) inside the contentitem, consider that it has the same task as the delegates in the views, for example you can make the button is square, circle, etc., that has an image as a background , etc., that has a color with respect to a state, etc. See doc.qt.io/qt-5.11/…

    – eyllanesc
    Nov 14 '18 at 19:18












  • @eyllanesc: I really don't understand a single word of whatever you are saying. I visited all the links you have shared. Anyway, thanks for the comments.

    – DJJ
    Nov 14 '18 at 19:22

















  • the contentItem is a way to customize a control, in your first code you are adding a child item to the button, and in the second customizing the same button, so both implementations have different objectives so both implementations are correct, so you must use one or the other according to what you want, so what output do you want?

    – eyllanesc
    Nov 14 '18 at 19:10











  • @eyllanesc: how did you conclude that contentItem is used to customize a Qml object? Reading the Qt documentation I couldn't conclude that. Does contentItem override the caption only?

    – DJJ
    Nov 14 '18 at 19:14











  • Qt Quick Controls II allows you to customize using that element, check the docs: see doc.qt.io/qt-5.11/qml-qtquick-controls2-control.html

    – eyllanesc
    Nov 14 '18 at 19:16











  • no, actually you should use the control text (button) inside the contentitem, consider that it has the same task as the delegates in the views, for example you can make the button is square, circle, etc., that has an image as a background , etc., that has a color with respect to a state, etc. See doc.qt.io/qt-5.11/…

    – eyllanesc
    Nov 14 '18 at 19:18












  • @eyllanesc: I really don't understand a single word of whatever you are saying. I visited all the links you have shared. Anyway, thanks for the comments.

    – DJJ
    Nov 14 '18 at 19:22
















the contentItem is a way to customize a control, in your first code you are adding a child item to the button, and in the second customizing the same button, so both implementations have different objectives so both implementations are correct, so you must use one or the other according to what you want, so what output do you want?

– eyllanesc
Nov 14 '18 at 19:10





the contentItem is a way to customize a control, in your first code you are adding a child item to the button, and in the second customizing the same button, so both implementations have different objectives so both implementations are correct, so you must use one or the other according to what you want, so what output do you want?

– eyllanesc
Nov 14 '18 at 19:10













@eyllanesc: how did you conclude that contentItem is used to customize a Qml object? Reading the Qt documentation I couldn't conclude that. Does contentItem override the caption only?

– DJJ
Nov 14 '18 at 19:14





@eyllanesc: how did you conclude that contentItem is used to customize a Qml object? Reading the Qt documentation I couldn't conclude that. Does contentItem override the caption only?

– DJJ
Nov 14 '18 at 19:14













Qt Quick Controls II allows you to customize using that element, check the docs: see doc.qt.io/qt-5.11/qml-qtquick-controls2-control.html

– eyllanesc
Nov 14 '18 at 19:16





Qt Quick Controls II allows you to customize using that element, check the docs: see doc.qt.io/qt-5.11/qml-qtquick-controls2-control.html

– eyllanesc
Nov 14 '18 at 19:16













no, actually you should use the control text (button) inside the contentitem, consider that it has the same task as the delegates in the views, for example you can make the button is square, circle, etc., that has an image as a background , etc., that has a color with respect to a state, etc. See doc.qt.io/qt-5.11/…

– eyllanesc
Nov 14 '18 at 19:18






no, actually you should use the control text (button) inside the contentitem, consider that it has the same task as the delegates in the views, for example you can make the button is square, circle, etc., that has an image as a background , etc., that has a color with respect to a state, etc. See doc.qt.io/qt-5.11/…

– eyllanesc
Nov 14 '18 at 19:18














@eyllanesc: I really don't understand a single word of whatever you are saying. I visited all the links you have shared. Anyway, thanks for the comments.

– DJJ
Nov 14 '18 at 19:22





@eyllanesc: I really don't understand a single word of whatever you are saying. I visited all the links you have shared. Anyway, thanks for the comments.

– DJJ
Nov 14 '18 at 19:22












1 Answer
1






active

oldest

votes


















2














Short answer: The contentItem is meant for customizing the control and replaces the existing implementation of the visual foreground element by your text.




Long answer:



A Quick Item has a so called "default property" - the data property. By definition, if you add an item as child of another item, it is instead assigned to the default property. Which means the following example:



Item 
Text text: "test1"



Is actually identical to:



Item 
data: [
Text text: "test2"
]



If you know look at your example, in the first variant, you simply append a child item to the root button. Since no further information is given, it is placed at the coordinates (0,0) within it's parent.



The contentItem property however is defined in the documentation as follows:




This property holds the visual content item.




In case of a Button it is an internally used Label to display the text property of the button. It exists to modify the appereance of the button.



In your second example, you "customize" the button by replacing the internal label with your custom Text - but without any code to properly position or fill the item. The correct way to declare a content item can be found in the customization guide:



Button 
id: control
text: qsTr("Button")

contentItem: Text
text: control.text
font: control.font
opacity: enabled ? 1.0 : 0.3
color: control.down ? "#17a81a" : "#21be2b"
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
elide: Text.ElideRight


// ...



You could either define it as part of a custom style, or create a MyButton.qml where do exactly this and can then use MyButton in other QML files, giving you a custom styled button whilest keeping the API intact (like beeing able to set the text via the text property etc.)



I hope this was sufficient enough to help you understand how it works.






share|improve this answer























  • Somebody speaks logic! Thank you @Felix! I get it now, contentItem is specific to every Qml component. I did read the links you provided couple of times but didn't really understand it the way you explained it! To add to your answer, the source code says it all. The Buttom.qml shows the customization for the Button text and background. See this: github.com/qt/qtquickcontrols2/blob/…

    – DJJ
    Nov 14 '18 at 20:13






  • 1





    Please note that the link you are referring to is the implementation of the "Default" design. check the subfolders in that directory for the button implementations for other styles, like Material, Universal, etc.

    – Felix
    Nov 14 '18 at 21:25










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307153%2fproperty-contentitem-in-qml%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Short answer: The contentItem is meant for customizing the control and replaces the existing implementation of the visual foreground element by your text.




Long answer:



A Quick Item has a so called "default property" - the data property. By definition, if you add an item as child of another item, it is instead assigned to the default property. Which means the following example:



Item 
Text text: "test1"



Is actually identical to:



Item 
data: [
Text text: "test2"
]



If you know look at your example, in the first variant, you simply append a child item to the root button. Since no further information is given, it is placed at the coordinates (0,0) within it's parent.



The contentItem property however is defined in the documentation as follows:




This property holds the visual content item.




In case of a Button it is an internally used Label to display the text property of the button. It exists to modify the appereance of the button.



In your second example, you "customize" the button by replacing the internal label with your custom Text - but without any code to properly position or fill the item. The correct way to declare a content item can be found in the customization guide:



Button 
id: control
text: qsTr("Button")

contentItem: Text
text: control.text
font: control.font
opacity: enabled ? 1.0 : 0.3
color: control.down ? "#17a81a" : "#21be2b"
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
elide: Text.ElideRight


// ...



You could either define it as part of a custom style, or create a MyButton.qml where do exactly this and can then use MyButton in other QML files, giving you a custom styled button whilest keeping the API intact (like beeing able to set the text via the text property etc.)



I hope this was sufficient enough to help you understand how it works.






share|improve this answer























  • Somebody speaks logic! Thank you @Felix! I get it now, contentItem is specific to every Qml component. I did read the links you provided couple of times but didn't really understand it the way you explained it! To add to your answer, the source code says it all. The Buttom.qml shows the customization for the Button text and background. See this: github.com/qt/qtquickcontrols2/blob/…

    – DJJ
    Nov 14 '18 at 20:13






  • 1





    Please note that the link you are referring to is the implementation of the "Default" design. check the subfolders in that directory for the button implementations for other styles, like Material, Universal, etc.

    – Felix
    Nov 14 '18 at 21:25















2














Short answer: The contentItem is meant for customizing the control and replaces the existing implementation of the visual foreground element by your text.




Long answer:



A Quick Item has a so called "default property" - the data property. By definition, if you add an item as child of another item, it is instead assigned to the default property. Which means the following example:



Item 
Text text: "test1"



Is actually identical to:



Item 
data: [
Text text: "test2"
]



If you know look at your example, in the first variant, you simply append a child item to the root button. Since no further information is given, it is placed at the coordinates (0,0) within it's parent.



The contentItem property however is defined in the documentation as follows:




This property holds the visual content item.




In case of a Button it is an internally used Label to display the text property of the button. It exists to modify the appereance of the button.



In your second example, you "customize" the button by replacing the internal label with your custom Text - but without any code to properly position or fill the item. The correct way to declare a content item can be found in the customization guide:



Button 
id: control
text: qsTr("Button")

contentItem: Text
text: control.text
font: control.font
opacity: enabled ? 1.0 : 0.3
color: control.down ? "#17a81a" : "#21be2b"
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
elide: Text.ElideRight


// ...



You could either define it as part of a custom style, or create a MyButton.qml where do exactly this and can then use MyButton in other QML files, giving you a custom styled button whilest keeping the API intact (like beeing able to set the text via the text property etc.)



I hope this was sufficient enough to help you understand how it works.






share|improve this answer























  • Somebody speaks logic! Thank you @Felix! I get it now, contentItem is specific to every Qml component. I did read the links you provided couple of times but didn't really understand it the way you explained it! To add to your answer, the source code says it all. The Buttom.qml shows the customization for the Button text and background. See this: github.com/qt/qtquickcontrols2/blob/…

    – DJJ
    Nov 14 '18 at 20:13






  • 1





    Please note that the link you are referring to is the implementation of the "Default" design. check the subfolders in that directory for the button implementations for other styles, like Material, Universal, etc.

    – Felix
    Nov 14 '18 at 21:25













2












2








2







Short answer: The contentItem is meant for customizing the control and replaces the existing implementation of the visual foreground element by your text.




Long answer:



A Quick Item has a so called "default property" - the data property. By definition, if you add an item as child of another item, it is instead assigned to the default property. Which means the following example:



Item 
Text text: "test1"



Is actually identical to:



Item 
data: [
Text text: "test2"
]



If you know look at your example, in the first variant, you simply append a child item to the root button. Since no further information is given, it is placed at the coordinates (0,0) within it's parent.



The contentItem property however is defined in the documentation as follows:




This property holds the visual content item.




In case of a Button it is an internally used Label to display the text property of the button. It exists to modify the appereance of the button.



In your second example, you "customize" the button by replacing the internal label with your custom Text - but without any code to properly position or fill the item. The correct way to declare a content item can be found in the customization guide:



Button 
id: control
text: qsTr("Button")

contentItem: Text
text: control.text
font: control.font
opacity: enabled ? 1.0 : 0.3
color: control.down ? "#17a81a" : "#21be2b"
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
elide: Text.ElideRight


// ...



You could either define it as part of a custom style, or create a MyButton.qml where do exactly this and can then use MyButton in other QML files, giving you a custom styled button whilest keeping the API intact (like beeing able to set the text via the text property etc.)



I hope this was sufficient enough to help you understand how it works.






share|improve this answer













Short answer: The contentItem is meant for customizing the control and replaces the existing implementation of the visual foreground element by your text.




Long answer:



A Quick Item has a so called "default property" - the data property. By definition, if you add an item as child of another item, it is instead assigned to the default property. Which means the following example:



Item 
Text text: "test1"



Is actually identical to:



Item 
data: [
Text text: "test2"
]



If you know look at your example, in the first variant, you simply append a child item to the root button. Since no further information is given, it is placed at the coordinates (0,0) within it's parent.



The contentItem property however is defined in the documentation as follows:




This property holds the visual content item.




In case of a Button it is an internally used Label to display the text property of the button. It exists to modify the appereance of the button.



In your second example, you "customize" the button by replacing the internal label with your custom Text - but without any code to properly position or fill the item. The correct way to declare a content item can be found in the customization guide:



Button 
id: control
text: qsTr("Button")

contentItem: Text
text: control.text
font: control.font
opacity: enabled ? 1.0 : 0.3
color: control.down ? "#17a81a" : "#21be2b"
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
elide: Text.ElideRight


// ...



You could either define it as part of a custom style, or create a MyButton.qml where do exactly this and can then use MyButton in other QML files, giving you a custom styled button whilest keeping the API intact (like beeing able to set the text via the text property etc.)



I hope this was sufficient enough to help you understand how it works.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 19:49









FelixFelix

4,77011839




4,77011839












  • Somebody speaks logic! Thank you @Felix! I get it now, contentItem is specific to every Qml component. I did read the links you provided couple of times but didn't really understand it the way you explained it! To add to your answer, the source code says it all. The Buttom.qml shows the customization for the Button text and background. See this: github.com/qt/qtquickcontrols2/blob/…

    – DJJ
    Nov 14 '18 at 20:13






  • 1





    Please note that the link you are referring to is the implementation of the "Default" design. check the subfolders in that directory for the button implementations for other styles, like Material, Universal, etc.

    – Felix
    Nov 14 '18 at 21:25

















  • Somebody speaks logic! Thank you @Felix! I get it now, contentItem is specific to every Qml component. I did read the links you provided couple of times but didn't really understand it the way you explained it! To add to your answer, the source code says it all. The Buttom.qml shows the customization for the Button text and background. See this: github.com/qt/qtquickcontrols2/blob/…

    – DJJ
    Nov 14 '18 at 20:13






  • 1





    Please note that the link you are referring to is the implementation of the "Default" design. check the subfolders in that directory for the button implementations for other styles, like Material, Universal, etc.

    – Felix
    Nov 14 '18 at 21:25
















Somebody speaks logic! Thank you @Felix! I get it now, contentItem is specific to every Qml component. I did read the links you provided couple of times but didn't really understand it the way you explained it! To add to your answer, the source code says it all. The Buttom.qml shows the customization for the Button text and background. See this: github.com/qt/qtquickcontrols2/blob/…

– DJJ
Nov 14 '18 at 20:13





Somebody speaks logic! Thank you @Felix! I get it now, contentItem is specific to every Qml component. I did read the links you provided couple of times but didn't really understand it the way you explained it! To add to your answer, the source code says it all. The Buttom.qml shows the customization for the Button text and background. See this: github.com/qt/qtquickcontrols2/blob/…

– DJJ
Nov 14 '18 at 20:13




1




1





Please note that the link you are referring to is the implementation of the "Default" design. check the subfolders in that directory for the button implementations for other styles, like Material, Universal, etc.

– Felix
Nov 14 '18 at 21:25





Please note that the link you are referring to is the implementation of the "Default" design. check the subfolders in that directory for the button implementations for other styles, like Material, Universal, etc.

– Felix
Nov 14 '18 at 21:25



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307153%2fproperty-contentitem-in-qml%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Darth Vader #20

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Ondo