Qt5之扩展对话框与在QLabel中显示中文的办法

//mainwindow.h

#ifndef MAINWINDOW_H

#define MAINWINDOW_H
 
  
#include
#include
#include
#include
#include
#include
#include
#include
class MainWindow : public QDialog
{
    Q_OBJECT
 
  
public:
    //QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));这种办法不起作用,
     //在    QT5中
    MainWindow(QWidget *parent = 0);
    void creatBaseInfo();
    void creatDetialInfo();
private:
    QWidget *baseWidget;
    QWidget *detailWidget;
private slots:
    void slotExtendWidget();
 
  
 
  
    //~MainWindow();
};
 
  
#endif // MAINWINDOW_H

 

 

//mainwindow.cpp

#include "mainwindow.h"

#include
#include
MainWindow::MainWindow(QWidget *parent)
    : QDialog(parent)
{
    creatBaseInfo();
    creatDetialInfo();
    QVBoxLayout *vLayout=new QVBoxLayout;
    vLayout->addWidget(baseWidget);
    vLayout->addWidget(detailWidget);
    vLayout->setSpacing(10);
 
  
    vLayout->setSizeConstraint(QLayout::SetFixedSize);
    //这一条代码很重要,当扩展对话框时避免布局发生变化
    this->setLayout(vLayout);
}
 
  
//MainWindow::~MainWindow()
//{
 
  
//}
 
  
void MainWindow::creatBaseInfo()
{
    baseWidget=new QWidget;
    QLabel *nameLabel=new QLabel;
    QString s1 =QString::fromUtf8("姓名");//这是在qlabel中显示中文的办法
    nameLabel->setText(s1);
 
  
    QLineEdit *nameLineEdit=new QLineEdit;
    QPushButton *okPushButton=new QPushButton;
    QString s2=QString::fromUtf8("确认");
    okPushButton->setText(s2);
 
  
    QLabel *sexLabel=new QLabel;
    QString s3=QString::fromUtf8("性别");
    sexLabel->setText(s3);
 
  
    QComboBox *sexCombox=new QComboBox;
    QString s4=QString::fromUtf8("男x");
    //不知为何”男“时,他就说后面有换行符
    QString s5=QString::fromUtf8("女x");
    sexCombox->insertItem(0,s4);
    sexCombox->insertItem(1,s5);
 
  
    QPushButton *detailPushButton=new QPushButton;
    QString s6=QString::fromUtf8("详细");
    detailPushButton->setText(s6);
 
  
    QGridLayout *mainGridLayout=new QGridLayout;
    mainGridLayout->addWidget(nameLabel,0,0);
    mainGridLayout->addWidget(nameLineEdit,0,1);
    mainGridLayout->addWidget(okPushButton,0,2);
    mainGridLayout->addWidget(sexLabel,1,0);
    mainGridLayout->addWidget(sexCombox,1,1);
    mainGridLayout->addWidget(detailPushButton,1,2);
    mainGridLayout->setMargin(10);
    mainGridLayout->setSpacing(10);
 
  
    baseWidget->setLayout(mainGridLayout);
 
  
 
  
 
  
    connect(detailPushButton,SIGNAL(clicked()),this,SLOT(slotExtendWidget()));
 
  
}
 
  
void MainWindow::creatDetialInfo()
{
    detailWidget=new QWidget;
    QLabel *ageLabel=new QLabel;
    QString s1=QString::fromUtf8("年龄");
    ageLabel->setText(s1);
 
  
    QLineEdit *ageLineEdit=new QLineEdit;
    ageLineEdit->setText("18");
 
  
    QGridLayout *mainGrid=new QGridLayout;
    mainGrid->addWidget(ageLabel,0,0);
    mainGrid->addWidget(ageLineEdit,0,1);
 
  
    detailWidget->setLayout(mainGrid);
    detailWidget->hide();
 
  
 
  
}
 
  
void MainWindow::slotExtendWidget()
{
    if(detailWidget->isHidden())
        detailWidget->show();
    else
        detailWidget->hide();
}

 

//main.cpp

#include "mainwindow.h"

#include 
 
  
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
 
  
    MainWindow w;
    w.show();
 
  
    return a.exec();
}
 
  

 

 

 

Qt5之扩展对话框与在QLabel中显示中文的办法_第1张图片

Qt5之扩展对话框与在QLabel中显示中文的办法_第2张图片

你可能感兴趣的:(Qt界面开发入门)