Qt、5.9、Windowsを使用しています。プログラムの開始後にstyleSheetプロパティを変更しようとしているので、ユーザーはプログラムの外観をカスタマイズできます。
styleSheetファイルを作成していません。代わりに、Qtのデザインモードで、変更しようとしている要素のプロパティに移動し、[styleSheet]プロパティをクリックして、Qtによって表示されるウィンドウに変更するプロパティを追加します。例については、画像1 *を確認してください。赤いボックスは、styleSheetプロパティを追加する場所を示し、緑のボックスは、styleSheetプロパティを追加できるポップアップウィンドウに移動するためにクリックする場所を示しています。
画像2 *は、この方法でstyleSheetプロパティを追加したときに.uiコードがどのように記述されるかを示しています。ここの赤いボックスは、コードが.uiファイルに自動的に書き込まれる方法を示しています。
menuBarのEditメニューに、「Appearance」と呼ばれるサブメニューを配置しました。プログラムの色のいくつかのプリセットオプションと、プログラムを自分でカスタマイズするオプションがあります。画像3 *では、メニューを見ることができます。
ユーザーが色オプションの1つをクリックすると、プログラムのstyleSheetプロパティが変更され、選択した色と色が一致するようになります。ここで問題が発生します。画像4は、次のカラーオプションをクリックするためのコードを示しています。
In the red square, the errors I have been getting, and the respective lines that produced them. In the yellow square, two of many trials I have made. In the blue square, the most seemingly effective tries. The one in line 21 wasn't compiled when I took the screenshot, but it only complained after compilation (but it did complained!). It says "notr" is not defined in this scope. So I tried declaring it. The one in line 22 is gave me an error when I tried "string notr", saying that the string was not defined in this scope. I included the <stdlib.h>, and changed it to "std::string notr", but it is still complaining. Like you can see in the red box, it says "expected primary-expression before 'notr'".
What does it mean? How do I fix this, so I can change the styleSheet properties of my elements? I thought of using styleSheet files for the pre-set colors, but I would like to avoid, if possible, using more files than needed. Also, how would the user be able o set a custom styleSheet, if the options are pre-set?
Thanks for the attention and help.
P.S.: Just as a bonus question. I'm trying changing basic colors first, then start advancing. Ultimately, I want to be able to reproduce something like Substance Look and Feel, its way of handling colors, making gradients, or giving the titleBar more than one color. Does anyone know what route to take? I believe styleSheets are too weak to reproduce those effects, so I would like to know how else could I change a program to look even more like Susbtance's skins. Thanks again.
*Images 1, 2 and 3 appear in the same image file, because StackOverflow wouldn't let me post more than 2 images.
The function setProperty()
according to the documentation:
Sets the value of the object's name property to value.
If the property is defined in the class using
Q_PROPERTY
then true is returned on success and false otherwise. If the property is not defined usingQ_PROPERTY
, and therefore not listed in the meta-object, it is added as a dynamic property and false is returned.
That is, the property must be defined in the class with that macro, but stylesheet does not meet those requirements so you will not be able to access with that method.
To dynamically change that property you must use the setStyleSheet()
. For example in your case:
void MainWindow::on_actionBlack_triggered()
{
centralWidget()->setStyleSheet("background-color: rgb(78, 78, 78);");
}
void MainWindow::on_actionBlack_Piano_triggered()
{
//another color
centralWidget()->setStyleSheet("background-color: rgb(111, 111, 111);");
}
void MainWindow::on_actionPok_dex_Orange_triggered()
{
//another color
centralWidget()->setStyleSheet("background-color: rgb(123, 22, 111);");
}
Note:
の使用std::string
は避けてくださいQString
。を使用する必要があります。
命令some_function(type_variable name_variable = some_value);
は有効なC / C ++構文ではありません
デザインxmlの場合:
<property name="styleSheet">
<string notr="true"> background-color: rgb (78, 78, 78);
font 10pt "Arial";
color: rgb(220, 220, 220);<string>
</property>
notr="true"
Qt翻訳システムを使用する場合、この文字列は考慮されないことを意味します:翻訳なし。
そのxmlは、コンパイラによって直接使用されることはありません。コンパイラは、これらの.uiファイルをui_yourdesign.hという名前の.hに変換します。
このsetStyleSheet()
関数はQWidgetの一部であるためQWidget
、QMenubar、QLineEdit、QTextEditなど、から継承するすべてのクラスで使用できます。
たとえば、表示するxmlは次のとおりです。
theBarメニューがプロパティに適用されていることを示します。その命令は、次のステートメントを使用してC ++を介して変更できます。
ui->menuBar->setStyleSheet("background-color: rgb (78, 78, 78); font 10pt; color: rgb(220, 220, 220);")
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加