nodeguy/fece615a.07aa2a4c.js
2021-12-11 14:39:55 +00:00

1 line
14 KiB
JavaScript

(window.webpackJsonp=window.webpackJsonp||[]).push([[402],{460:function(e,t,n){"use strict";n.r(t),n.d(t,"frontMatter",(function(){return i})),n.d(t,"metadata",(function(){return a})),n.d(t,"rightToc",(function(){return r})),n.d(t,"default",(function(){return p}));var o=n(2),s=n(6),l=(n(0),n(469)),i={sidebar_label:"Styling",title:"Styling"},a={unversionedId:"guides/styling",id:"guides/styling",isDocsHomePage:!1,title:"Styling",description:"With NodeGui, you can style a widget to your needs. If you are familiar with CSS in the web world you would feel right at home. All widgets have a method setInlineStyle for setting inline styles for the respective widget. The style names and values usually match how CSS works on the web.",source:"@site/docs/guides/styling.md",slug:"/guides/styling",permalink:"/docs/guides/styling",editUrl:"https://github.com/nodegui/nodegui/edit/master/website/docs/guides/styling.md",version:"current",sidebar_label:"Styling",sidebar:"guides",previous:{title:"Learn the Basics",permalink:"/docs/guides/tutorial"},next:{title:"Layout",permalink:"/docs/guides/layout"}},r=[{value:"Overview",id:"overview",children:[]},{value:"Global styles",id:"global-styles",children:[]},{value:"Inline styles",id:"inline-styles",children:[]},{value:"Selectors",id:"selectors",children:[]},{value:"Pseudo states",id:"pseudo-states",children:[]},{value:"Cascading",id:"cascading",children:[]},{value:"Properties",id:"properties",children:[]},{value:"Supported properties",id:"supported-properties",children:[]},{value:"Advanced usage (Setting QObject Properties)",id:"advanced-usage-setting-qobject-properties",children:[]}],c={rightToc:r};function p(e){var t=e.components,n=Object(s.a)(e,["components"]);return Object(l.b)("wrapper",Object(o.a)({},c,n,{components:t,mdxType:"MDXLayout"}),Object(l.b)("p",null,"With NodeGui, you can style a widget to your needs. If you are familiar with CSS in the web world you would feel right at home. All widgets have a method ",Object(l.b)("inlineCode",{parentName:"p"},"setInlineStyle")," for setting inline styles for the respective widget. The style names and values usually match how CSS works on the web."),Object(l.b)("p",null,"Here's an example:"),Object(l.b)("pre",null,Object(l.b)("code",Object(o.a)({parentName:"pre"},{className:"language-js"}),'const { QLabel, QMainWindow } = require("@nodegui/nodegui");\n\nconst win = new QMainWindow();\n\nconst label = new QLabel(win);\nlabel.setText("Hello world");\nlabel.setInlineStyle("color: green; background-color: white;");\n\nwin.show();\nglobal.win = win;\n')),Object(l.b)("h2",{id:"overview"},"Overview"),Object(l.b)("p",null,"NodeGui makes use of ",Object(l.b)("a",Object(o.a)({parentName:"p"},{href:"https://doc.qt.io/qt-5/stylesheet-syntax.html"}),"Qt's stylesheet")," for styling. Qt Style Sheet terminology and syntactic rules are almost identical to those of HTML CSS. Additionally, NodeGui adds support for layout using flex properties like align-items, justify-content, etc. Flexbox layout support is added using ",Object(l.b)("a",Object(o.a)({parentName:"p"},{href:"https://github.com/facebook/yoga"}),"facebook's yoga library"),"."),Object(l.b)("p",null,"You would write your style properties in a string and pass it to the NodeGui widgets either via global styles or inline styles (similar to how it works in the web)."),Object(l.b)("h2",{id:"global-styles"},"Global styles"),Object(l.b)("p",null,"Lets take a look at an example:"),Object(l.b)("pre",null,Object(l.b)("code",Object(o.a)({parentName:"pre"},{className:"language-js"}),'const { QLabel, FlexLayout, QWidget } = require("@nodegui/nodegui");\n\nconst view = new QWidget();\nview.setObjectName("rootView");\nview.setLayout(new FlexLayout());\n\nconst label = new QLabel();\nlabel.setObjectName("helloLabel");\nlabel.setText("Hello");\n\nconst label2 = new QLabel();\nlabel2.setObjectName("worldLabel");\nlabel2.setText("World");\n\nview.layout.addWidget(label);\nview.layout.addWidget(label2);\n\nview.setStyleSheet(`\n #helloLabel {\n color: red;\n padding: 10px;\n }\n #worldLabel {\n color: green;\n padding: 10px;\n }\n #rootView {\n background-color: black;\n }\n`);\nview.show();\n(global as any).view = view;\n\n')),Object(l.b)("p",null,"In the case of global stylesheet you can define all your style properties in a stylesheet string and the tell the root view or window to set it as a stylsheet for it and its child widgets. The only difference here from web is that you can set a stylesheet on a widget at any level in the whole tree of widgets, the stylesheet will affect the widget and its children."),Object(l.b)("p",null,"In the above example, in order to reference a widget in a stylesheet we will assign it a ",Object(l.b)("inlineCode",{parentName:"p"},"objectName")," using setObjectName instance method. Think of objectName as something similar to an ",Object(l.b)("inlineCode",{parentName:"p"},"id")," in the case of web. Now using the objectName you could reference the widget in the stylesheet and set style properties on them. Do not worry about the layout stuff that is going on here, that will be covered in the next section."),Object(l.b)("p",null,"Global stylesheet really becomes powerful when you use things like pseudo-selectors (hover, checked, etc). It also has helps in implementing cascaded styles which allow you to style a group of widgets at once. We will see more about these features below."),Object(l.b)("blockquote",null,Object(l.b)("p",{parentName:"blockquote"},"More details on all the features and the syntax can be found here: ",Object(l.b)("a",Object(o.a)({parentName:"p"},{href:"https://doc.qt.io/qt-5/stylesheet-syntax.html"}),"https://doc.qt.io/qt-5/stylesheet-syntax.html"))),Object(l.b)("h2",{id:"inline-styles"},"Inline styles"),Object(l.b)("p",null,"Lets look at this example again:"),Object(l.b)("pre",null,Object(l.b)("code",Object(o.a)({parentName:"pre"},{className:"language-js"}),'const { QLabel, QMainWindow } = require("@nodegui/nodegui");\n\nconst win = new QMainWindow();\n\nconst label = new QLabel(win);\nlabel.setText("Hello world");\nlabel.setInlineStyle("color: green; background-color: white;");\n\nwin.show();\nglobal.win = win;\n')),Object(l.b)("p",null,"In most cases it would be easier to style the widgets inline. NodeGui supports inline styling using ",Object(l.b)("inlineCode",{parentName:"p"},"setInlineStyle")," instance method. Inline styles will only affect the widget to which the style is applied to and is often easier to understand and manage. All properties you use in the global stylesheet are available in inline styles as well."),Object(l.b)("h2",{id:"selectors"},"Selectors"),Object(l.b)("p",null,"NodeGui style sheets support all the selectors defined in CSS2.\nSome examples include:"),Object(l.b)("pre",null,Object(l.b)("code",Object(o.a)({parentName:"pre"},{className:"language-css"}),"* {\n color: blue;\n}\n\nQPushButton {\n padding: 10px;\n}\n\n#okButton {\n margin: 10px;\n}\n\n#mainView > QPushButton {\n margin: 10px;\n}\n")),Object(l.b)("p",null,"To see a complete list of selectors see here: ",Object(l.b)("a",Object(o.a)({parentName:"p"},{href:"https://doc.qt.io/qt-5/stylesheet-syntax.html#selector-types"}),"https://doc.qt.io/qt-5/stylesheet-syntax.html#selector-types")),Object(l.b)("h2",{id:"pseudo-states"},"Pseudo states"),Object(l.b)("p",null,"Like in the web, you can style your widget based on its state. An example would be, you might want the color of the button text to be red when its hovered upon. These are possible with pseudo states. Pseudo-states appear at the end of the selector, with a colon (:) in between."),Object(l.b)("pre",null,Object(l.b)("code",Object(o.a)({parentName:"pre"},{className:"language-css"}),"#okButton:hover {\n color: red;\n}\n")),Object(l.b)("blockquote",null,Object(l.b)("p",{parentName:"blockquote"},"More details here : ",Object(l.b)("a",Object(o.a)({parentName:"p"},{href:"https://doc.qt.io/qt-5/stylesheet-syntax.html#pseudo-states"}),"https://doc.qt.io/qt-5/stylesheet-syntax.html#pseudo-states"))),Object(l.b)("h2",{id:"cascading"},"Cascading"),Object(l.b)("p",null,"Style sheets can be set on the parent widgets and on child widgets. An arbitrary widget's effective style sheet is obtained by merging the style sheets set on the widget's ancestors (parent, grandparent, etc.)."),Object(l.b)("p",null,"When conflicts arise, the widget's own inline style sheet is always preferred to any inherited style sheet, irrespective of the specificity of the conflicting rules. Likewise, the parent widget's style sheet is preferred to the grandparent's, etc."),Object(l.b)("p",null,"The behaviour is similar to what we see on the web."),Object(l.b)("blockquote",null,Object(l.b)("p",{parentName:"blockquote"},"For more in depth examples see here: ",Object(l.b)("a",Object(o.a)({parentName:"p"},{href:"https://doc.qt.io/qt-5/stylesheet-syntax.html#cascading"}),"https://doc.qt.io/qt-5/stylesheet-syntax.html#cascading"))),Object(l.b)("h2",{id:"properties"},"Properties"),Object(l.b)("p",null,"NodeGui style sheets is a css string."),Object(l.b)("p",null,"For example:"),Object(l.b)("pre",null,Object(l.b)("code",Object(o.a)({parentName:"pre"},{}),"const textStyle = `\n color: 'green';\n padding: 12;\n height: '100%';\n`;\n\n")),Object(l.b)("p",null,"Here if you look carefully, you would notice that there are some differences in the way we write style properties as compared to web.\nThe quotes you see around ",Object(l.b)("inlineCode",{parentName:"p"},"'green'")," and ",Object(l.b)("inlineCode",{parentName:"p"},"'100%'")," are necessary so that Qt doesnt interpret them as numbers.\nSo the rule of thumb is that any integer based property like margin, border, etc can be written without quotes while any string property, it is better to surround them with quotes. PS: Qt does recognise some string based properties without quotes also."),Object(l.b)("h2",{id:"supported-properties"},"Supported properties"),Object(l.b)("p",null,"Since we are not running inside a web browser, there are few differences in the properties you could use in NodeGui vs in web."),Object(l.b)("p",null,"The complete list is detailed here: ",Object(l.b)("a",Object(o.a)({parentName:"p"},{href:"https://doc.qt.io/qt-5/stylesheet-reference.html#list-of-properties"}),"https://doc.qt.io/qt-5/stylesheet-reference.html#list-of-properties")),Object(l.b)("p",null,"Apart from the properties listed in the link, NodeGui also supports layout properties related to Flex. You can use all flex properties such as align-items, justify-content, flex, etc on all widgets. ",Object(l.b)("a",Object(o.a)({parentName:"p"},{href:"/docs/guides/layout"}),"The layout styling will be converted in more detail in the section: Layout.")),Object(l.b)("h2",{id:"advanced-usage-setting-qobject-properties"},"Advanced usage (Setting QObject Properties)"),Object(l.b)("p",null,"In Qt, every widget has certain properties set on them using something called as ",Object(l.b)("a",Object(o.a)({parentName:"p"},{href:"https://doc.qt.io/qt-5/qobject.html#Q_PROPERTY"}),"Q_PROPERTY"),". There are many q-properties that are defined on each widget already. You can also define custom qproperties in the native C++ code yourself too. What does it have to do with styling ? The thing is some of these properties can be altered using qt stylesheet. In Qt's terminology, these properties are called designable properties."),Object(l.b)("p",null,"For example:"),Object(l.b)("pre",null,Object(l.b)("code",Object(o.a)({parentName:"pre"},{className:"language-css"}),"MyLabel {\n qproperty-alignment: AlignCenter;\n}\nMyGroupBox {\n qproperty-titlecolor: rgb(100, 200, 100);\n}\nQPushButton {\n qproperty-iconsize: 20px 20px;\n}\n")),Object(l.b)("p",null,'You can discover these properties by following Qt\'s documentation or by running a simple google search like "center text in QLabel using stylesheet in Qt". These are advanced properties and in practice will come in use rarely but its good to know.'),Object(l.b)("blockquote",null,Object(l.b)("p",{parentName:"blockquote"},"More details : ",Object(l.b)("a",Object(o.a)({parentName:"p"},{href:"https://doc.qt.io/qt-5/stylesheet-syntax.html#setting-qobject-properties"}),"https://doc.qt.io/qt-5/stylesheet-syntax.html#setting-qobject-properties"))),Object(l.b)("hr",null),Object(l.b)("p",null,"In this section, we mostly covered the paint properties in the NodeGui stylesheet. The next section would cover on how you can use flex to layout your widgets with stylesheet."))}p.isMDXComponent=!0},469:function(e,t,n){"use strict";n.d(t,"a",(function(){return b})),n.d(t,"b",(function(){return h}));var o=n(0),s=n.n(o);function l(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function i(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);t&&(o=o.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,o)}return n}function a(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?i(Object(n),!0).forEach((function(t){l(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):i(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function r(e,t){if(null==e)return{};var n,o,s=function(e,t){if(null==e)return{};var n,o,s={},l=Object.keys(e);for(o=0;o<l.length;o++)n=l[o],t.indexOf(n)>=0||(s[n]=e[n]);return s}(e,t);if(Object.getOwnPropertySymbols){var l=Object.getOwnPropertySymbols(e);for(o=0;o<l.length;o++)n=l[o],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(s[n]=e[n])}return s}var c=s.a.createContext({}),p=function(e){var t=s.a.useContext(c),n=t;return e&&(n="function"==typeof e?e(t):a(a({},t),e)),n},b=function(e){var t=p(e.components);return s.a.createElement(c.Provider,{value:t},e.children)},d={inlineCode:"code",wrapper:function(e){var t=e.children;return s.a.createElement(s.a.Fragment,{},t)}},u=s.a.forwardRef((function(e,t){var n=e.components,o=e.mdxType,l=e.originalType,i=e.parentName,c=r(e,["components","mdxType","originalType","parentName"]),b=p(n),u=o,h=b["".concat(i,".").concat(u)]||b[u]||d[u]||l;return n?s.a.createElement(h,a(a({ref:t},c),{},{components:n})):s.a.createElement(h,a({ref:t},c))}));function h(e,t){var n=arguments,o=t&&t.mdxType;if("string"==typeof e||o){var l=n.length,i=new Array(l);i[0]=u;var a={};for(var r in t)hasOwnProperty.call(t,r)&&(a[r]=t[r]);a.originalType=e,a.mdxType="string"==typeof e?e:o,i[1]=a;for(var c=2;c<l;c++)i[c]=n[c];return s.a.createElement.apply(null,i)}return s.a.createElement.apply(null,n)}u.displayName="MDXCreateElement"}}]);