2016-09-29 7 views
-1

main.cppにアプリケーションの実行中にスタイルシートファイルをアプリケーション上でリセットするにはどうすればよいですか?

int main(int argc, char *argv[]){ 
QApplication app(argc, argv); 

QFile data("://stylesheet.qss"); // this is a stylesheet file in images.qrc file 
    QString style; 
    if (data.open(QFile::ReadOnly)) 
    { 
     QTextStream styleIn(&data); 
     style = styleIn.readAll(); 
     data.close(); 
     app.setStyleSheet(style); // I want this line to restart with different style-sheet file 
    } 
app.setWindowIcon(QIcon("://images/BEL.jpg")); 

MainWindow w; 
w.setFixedSize(500,350); 
w.showMaximized(); 
return app.exec();} 

theme.cpp

theme::theme(QWidget *parent) : 
QDialog(parent), 
ui(new Ui::theme) {  
ui->setupUi(this); 
ui->apply_button->setDefault(1); 
QButtonGroup *radio=new QButtonGroup; 
radio->setExclusive(true); 
radio->addButton(ui->radio_blue,2); 
radio->addButton(ui->radio_grey,3); 
connect(radio,SIGNAL(buttonClicked(int)),this,SLOT(function(int)));} 
theme::~theme() 
{ 
delete ui; 
} 
void theme::on_pushButton_2_clicked() 
{ 
this->close(); 
} 
    void theme::function(int value) 
    { if(value==2){ 
    qDebug()<<"blue";} 
    else if(value==3){ 
    qDebug()<<"green";} 
else 
    qDebug()<< "error"; 
    } 

私はmain.cppにしてtheme.cppや他の多くのファイルを持っています。テーマウィンドウに2つのラジオボタンがあります。私は灰色と青色の両方のための2つのスタイルシートファイルを持っています。私がボタンをチェックすると、main.cppのようにアプリケーション全体に適用されるはずです。

問題は、アプリケーションを起動するときにファイルを1回だけ適用することです。しかし、私は他のボタンをチェックするときにそれを実行させたい。とにかくmain.cppで特定のボタンが押され、そのボタンのスタイルシートファイルを適用してアプリケーションを再起動できるようになったのですか?

+0

あなたの正確な問題は何ですか?あなたはファイルを読むことができませんか?スタイルシートは適用できませんか? –

+0

私の問題の解決を得ました –

+0

答えを提供するか質問を削除してください –

答えて

1
theme::theme(QWidget *parent) : 
    QDialog(parent), 
    ui(new Ui::theme) 
    { 
    ui->setupUi(this); 
    switch(radio) 
{ 
case 1: 
    ui->radio_blue->setChecked(1); 
    break; 
case 2: 
    ui->radio_red->setChecked(1); 
    break; 
case 3: 
    ui->radio_green->setChecked(1); 
} 
QButtonGroup *radio=new QButtonGroup; 
radio->setExclusive(true); 
radio->addButton(ui->radio_blue,2); 
radio->addButton(ui->radio_red,3); 
radio->addButton(ui->radio_green,4); 
connect(radio,SIGNAL(buttonClicked(int)),this,SLOT(function(int))); 
qApp->installEventFilter(this); 

} 
theme::~theme() 
{ 
delete ui; 
} 

void theme:: function(int value) 
    { 
if(value==2) 
{ 
    qDebug()<<"blue"; 
    theme_val=2; 

} 
else if(value==3) 
{ 
    qDebug()<<"red"; 
    theme_val=3; 


} 
else if(value==4) 
{ 
    qDebug()<< "green"; 
    theme_val=4; 

} 
else 
    qDebug()<< "error"; 

    } 
    void theme::on_apply_button_clicked() 
    { 
if(theme_val==2) 
{ 
    qDebug()<< "blue"; 
    QFile data1("://stylesheet.qss"); 
    QString style1; 
    if (data1.open(QFile::ReadOnly)) 
    { 
     QTextStream styleIn(&data1); 
     style1 = styleIn.readAll(); 
     data1.close(); 
     qApp->setStyleSheet(style1); 
    } 
    radio=1; 
} 
else if(theme_val==3) 
{ 
    qDebug()<< "red"; 
    QFile data("://stylesheet1.qss"); 
    QString style; 
    if (data.open(QFile::ReadOnly)) 
    { 
     QTextStream styleIn(&data); 
     style = styleIn.readAll(); 
     data.close(); 
     qApp->setStyleSheet(style); 
    } 
    radio=2; 
} 
else if(theme_val==4) 
{ 
    qDebug()<< "green"; 
    QFile data2("://stylesheet2.qss"); 
    QString style2; 
    if (data2.open(QFile::ReadOnly)) 
    { 
     QTextStream styleIn(&data2); 
     style2 = styleIn.readAll(); 
     data2.close(); 
     qApp->setStyleSheet(style2); 
    } 
    radio=3; 
} 
else 
    qDebug()<< "errror" ; 
close(); 
} 
関連する問題