简体   繁体   中英

Getting rid of repeating code?

I have a long-ish line of code that repeats pretty often:

ui->someLabel1->setStyleSheet(QStringLiteral("QLabel{color: transparent;}"));
ui->someLabel2->setStyleSheet(QStringLiteral("QLabel{color: transparent;}"));

The issue is that the someLabel part is the only thing different in every line of that code. How would I go about setting up a function to cut down on this long line of code?

Here's an example of what I tried:

myClass.h:

protected:
  void clearLabel(QLabel* label);

myClass.cpp

void myClass::clearLabel(QLabel* label){
  ui->label->setStyleSheet(QStringLiteral("QLabel{color: transparent;}"));
}

main.cpp

if (someFlag == 1){
  clearLabel(someLabel1);
  } else {
    clearLabel(someLabel2);
}

This comes back with a someLabel was not declared in this scope error.

Am I going about this wrong? The only part of that long line of code I need to replace is someLabel .

In main.cpp, you have to call

clearLabel(ui->someLabel2);

The ui-> part is really important, otherwise you don't give the funtion an existing object.

You need to use a macro (Least recommended) or template here.

One example would be:

#define CLEAR_LABEL(LABEL) ui->LABEL->setStyleSheet(QStringLiteral("QLabel{color: transparent;}"));

if (someFlag == 1){
  CLEAR_LABEL(someLabel1);
} else {
  CLEAR_LABEL(someLabel2);
}

If you want to stick with the function (Recommended), do the following modifications:

void myClass::clearLabel(QLabel* label){
  label->setStyleSheet(QStringLiteral("QLabel{color: transparent;}"));
  // label instead of ui->label
}

...

if (someFlag == 1){
  clearLabel(ui->someLabel1);  // ui->somelabel1 instead of somelabel1
} else {
  clearLabel(ui->someLabel2);  // ui->somelabel2 instead of somelabel2
}

Assuming your ui class is called Ui , the fix for your current code would be something like

void myClass::clearLabel(QLabel* Ui::*label){
  (ui->*label)->setStyleSheet(QStringLiteral("QLabel{color: transparent;}"));
}

myclass.clearLabel(&Ui::someLabel1);
myclass.clearLabel(&Ui::someLabel2);

although it's hard to recommend the use of pointers to member, you should probably rethink your design and have some kind of collection for the labels you can perform uniform operations over.

If you set this stylesheet on every QLabel in you application, you could set this stylesheet directly on the application with qApp->setStylesheet(QStringLiteral("QLabel{color: transparent;}"));

If there are many labels that need to have this style, but not all, you could set a dynamic property on theses labels in Qt Designer. In the designer, select the QLabel and click on the big green plus sign in the properties section and selecting "Boolean value...". Now set the name to "transparent" and check this property.

Qt Designer动态属性

If you set the application stylesheet like this:

qApp->setStyleSheet("QLabel[transparent=true]{ color: transparent; }");

all QLabels with this property should show up as desired.

The advantage of this is that you don't need to write a line of code to style a label in this way.

Or can try to add the QLabel s to a QList<QLabel*> . Something like that:

QList<QLabel*> mylist;

mylist << ui->label << ui->label_2 << ui->label_3;

for (int i=0;i<3;i++) {
    mylist.at(i)->setStyleSheet(QStringLiteral("QLabel{color: transparent;}"));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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