各位大佬,請問在兩個QDialog傳值切換語言的時候,比如說主視窗運行起來了,點擊設定視窗,選擇英文后,如何讓主視窗在的QLabel也顯示英文呢?具體兩個QDialog的子類,如下代碼:
class parentDialog: public QDialog
{
Q_OBJECT
public:
parentDialog(QWidget *parent = nullptr);//設定表單無邊界
~parentDialog();
protected:
QPoint m_curPoint;
void paintEvent(QPaintEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
bool eventFilter(QObject *watched, QEvent *event);
};
class mainDialog: public parentDialog
{
Q_OBJECT
public:
mainDialog(QWidget *parent = nullptr);//設定表單無邊界
~mainDialog();
void initUi();
private:
QPushButton *m_closeBtn;
QPushButton *m_settingsBtn;
QLabel *m_labelTip;
private slots:
void slotShowSettingsDlg();
}
class SettingsDialog : public QDialog
{
Q_OBJECT
public:
SettingsDialog (QWidget *parent = nullptr);//設定表單無邊界
~SettingsDialog ();
void initUi();
private:
QPushButton *m_closeBtn;
QComboBox *m_comBox;
QLabel *m_label;
QPushButton *m_okBtn;
private slots:
void slotClickedBtnOkFun();
}
//
void mainDialog::initUi()
{
m_closeBtn = new QPushButton(this);
m_closeBtn ->setFocusPolicy(Qt::NoFocus); //不作為默認焦點
m_closeBtn ->setToolTip("關閉");
m_closeBtn ->installEventFilter(this);
QImage closeImg(":/res/pwd/Closebtn.png");
m_closeBtn ->resize(closeImg.size());
m_closeBtn ->move(this->width()*99/100-closeImg.width(), this->height()*1/100);
connect(m_btnClose, &QPushButton::clicked, this, &mainDialog::reject);
m_settingsBtn = new QPushButton(this);
m_settingsBtn ->setFocusPolicy(Qt::NoFocus);
m_settingsBtn ->setToolTip("設定");
m_settingsBtn ->installEventFilter(this);
QImage settingsImg(":/res/main/SettingBtn.png");
m_settingsBtn ->resize(settingsImg.size());
m_settingsBtn ->move(this->width()*99/100-closeImg.width()-settingsImg.width(), this->height()*1/100);
connect(m_btnSettings, &QPushButton::clicked, this, &mainDialog::slotShowSettingsDlg);
m_labelTip = new QLabel("你好,this);
m_labelTip ->setAlignment(Qt::AlignLeft);
m_labelTip ->resize(this->width()*60/100, this->height()*20/100);
m_labelTip ->move(this->width()*16/100, this->height()*6/100);
}
void mainDialog::slotShowSettingsDlg()
{
SettingsDialog setsDlg;
int res = setsDlg.exec();
if(res != QDialog::Accepted)
{
return;
}
}
void SettingsDialog::initUi()
{
m_closeBtn = new QPushButton(this);
m_closeBtn ->setFocusPolicy(Qt::NoFocus); //不作為默認焦點
m_closeBtn ->setToolTip("關閉");
m_closeBtn ->installEventFilter(this);
QImage closeImg(":/res/pwd/Closebtn.png");
m_closeBtn ->resize(closeImg.size());
m_closeBtn ->move(this->width()*99/100-closeImg.width(), this->height()*1/100);
connect(m_btnClose, &QPushButton::clicked, this, &SettingsDialog::reject);
m_comBox = new QComboBox(this);
m_comBox ->addItem("中文");
m_comBox ->addItem("英文");
QImage langImg(":/res/sets/comboBoxBkg.png");
m_comBox ->resize(langImg.size());
m_comBox ->move(this->width()*60/100 , this->height()*50/100);
m_okBtn= new QPushButton("確定", this);
m_btnOk->setFocusPolicy(Qt::NoFocus);
m_btnOk->installEventFilter(this);
QImage okImg(":/res/sets/btnOk0.png");
m_btnOk->resize(okImg.size());
m_btnOk->move(this->width()*51/100, this->height()*76/100);
connect(m_btnOk, &QPushButton::clicked, this, &settingsDlg::slotClickedBtnOkFun);
}
void SettingsDialog::slotClickedBtnOkFun()
{
//如上,選擇英文后,如何給主視窗mainDialog傳遞資訊,讓m_labelTip的文本資訊也變成英文呢?
}
還請各位大佬指點一下,小弟在此多謝了!
uj5u.com熱心網友回復:
slotClickedBtnOkFun()發送信號 SettingsDialog setsDlg后加connect(...) 槽函式為修改mainDialog的語言SettingsDialog 創建可以作為成員變數的指標物件 在mainDialog::initUi就創建出來 先hide 信號槽在這個時候進行connect setsDlg的時候再show
uj5u.com熱心網友回復:
大佬,請問mainDialog這個主視窗不是先初始出來,那在選擇SettingsDialog的語言設定后,它怎么能讓mainDialog類似剛開始一樣再初始化,切換語言呢?
雖然hide可以隱藏視窗,但是切換語言后,再顯示show視窗,好像還是沒辦法讓mainDialog上的QLabel切換成英文。除非再設定QLabel的文本資訊切換。
uj5u.com熱心網友回復:
槽函式在mainDialog 不是能拿到m_labelTip指標物件嗎 m_labelTip重新setText就行了
Qt正常的語言切換是加載翻譯檔案 切換不同的翻譯檔案時 會觸發changeEvent的LanguageChange狀態 具體你百度Qt 多語言
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/278254.html
標籤:Qt
上一篇:關于樣式單顏色加載疑問
