QT4编程过程中遇到的问题及解决办法
2014-03-15 16:48
826 查看
1、QLineEdit显示内容的格式函数:
QLineEdit *lineEditPassword = new QLineEdit;
lineEditPassword -> setEchoMode(QLineEdit::Password);
注:
QLineEdit::Normal0 Display characters as they are entered. This is the default.
QLineEdit::NoEcho1 Do not display anything. This may be appropriate for passwords where even the length of the password should be kept secret.
QLineEdit::Password2 Display asterisks instead of the characters actually entered.
QLineEdit::PasswordEchoOnEdit3 Display characters as they are entered while editing otherwise display asterisks.
2、设置窗体大小:
(1)this->setFixedSize(width,height); //设置窗体固定大小
(2)this->resize(QSize(320,240));
(3)QSize Mywindow::sizeHint() const
{
return QSize( 800, 600 );
}
注意:要#include<QSize>
3、qss的设置使用:
(1)建立文本文件,写入样式表内容,更改文件后缀名为qss;
(2)在工程中新建资源文件*.qrc,将qss文件加入资源文件qrc中,此处注意prefix最好为"/",否则在调用qss文件时会找不到文件;
(3)通过传入路径\文件名的方式创建一个QFile对象,以readonly的方式打开,然后readAll,最后qApp->setStyleSheet就可以使qss生效。
qss文件路径及前缀设置如图所示:
我的代码:
注:不知道为什么qApp->setStyleSheet(qss.readALL());不可以
4、在Label部件中显示图片
(1)使用stylesheet:
(2)
效果如下:
5、设置坐标
以Label为例:
6、设置大小:
以Label为例:
设置固定大小:
7、Label中的文字居中:
8、QPushButton设置背景图片:
这样设置后按钮大小不变,图片也固定了。
效果如下所示:
9、不能显示中文:
在main.cpp文件中写入
10、解决QCalendarWidget不能显示“一二三四五六日”的问题:
QCalendarWidget本地化的过程中,需要显示的进行 setLocale 的设定。
效果如下:
11、去掉QMainwindow的标题栏
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowFlags(Qt::FramelessWindowHint);//去掉标题栏
this->setGeometry(QRect(950, 55, 350, 250));//可设置窗口显示的方位与大小
//this->setWindowOpacity(0.7);//设置透明1-全体透明
//this->setAttribute(Qt::WA_TranslucentBackground, true);//设置透明2-窗体标题栏不透明,背景透明
this->resize(300,300);//显示大小
}
QLineEdit *lineEditPassword = new QLineEdit;
lineEditPassword -> setEchoMode(QLineEdit::Password);
注:
QLineEdit::Normal0 Display characters as they are entered. This is the default.
QLineEdit::NoEcho1 Do not display anything. This may be appropriate for passwords where even the length of the password should be kept secret.
QLineEdit::Password2 Display asterisks instead of the characters actually entered.
QLineEdit::PasswordEchoOnEdit3 Display characters as they are entered while editing otherwise display asterisks.
2、设置窗体大小:
(1)this->setFixedSize(width,height); //设置窗体固定大小
(2)this->resize(QSize(320,240));
(3)QSize Mywindow::sizeHint() const
{
return QSize( 800, 600 );
}
注意:要#include<QSize>
3、qss的设置使用:
(1)建立文本文件,写入样式表内容,更改文件后缀名为qss;
(2)在工程中新建资源文件*.qrc,将qss文件加入资源文件qrc中,此处注意prefix最好为"/",否则在调用qss文件时会找不到文件;
(3)通过传入路径\文件名的方式创建一个QFile对象,以readonly的方式打开,然后readAll,最后qApp->setStyleSheet就可以使qss生效。
qss文件路径及前缀设置如图所示:
我的代码:
QFileqss(":/my.qss");//路径为应用程序所在目录开始
qss.open(QFile::ReadOnly);
QStringqsss=QLatin1String(qss.readAll());
qApp->setStyleSheet(qsss);
qss.close();
注:不知道为什么qApp->setStyleSheet(qss.readALL());不可以
4、在Label部件中显示图片
(1)使用stylesheet:
QLabel*label=newQLabel(this);
label->setStyleSheet("border-image:url(:/fire_red)");//注意路径
(2)
QLabel*label=newQLabel(this);
QStringpath=":/fire_red.png";//相对项目文件
QPixmapimg(path);
label->setPixmap(path);
效果如下:
5、设置坐标
以Label为例:
label->move(20,24);
6、设置大小:
以Label为例:
Label->resize(QSize(46,20));
设置固定大小:
Label->setFixedSize(QSize(46,20));
7、Label中的文字居中:
Label->setAlignment(Qt::AlignCenter);
8、QPushButton设置背景图片:
backPushButton->setFixedSize(QSize(30,30));
backPushButton->setIcon(QIcon(":/back1.png"));
backPushButton->setIconSize(QSize(30,30));
这样设置后按钮大小不变,图片也固定了。
效果如下所示:
9、不能显示中文:
在main.cpp文件中写入
QStringsPath=a.applicationDirPath();
sPath+=QString("/plugins");
a.addLibraryPath(sPath);
QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
QTextCodec::setCodecForLocale(QTextCodec::codecForName("GBK"));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GBK"));
注:该段代码加在 QApplicationa(argc,argv);之后
然后在程序的exe目录下拷贝plugins文件夹,就可以了。之前我试过网上的很多方法都不行,有的方法只能显示部分中文。现在这种方法亲身实践可行!!!
10、解决QCalendarWidget不能显示“一二三四五六日”的问题:
QCalendarWidget本地化的过程中,需要显示的进行 setLocale 的设定。
calendar->setLocale(QLocale(QLocale::Chinese,QLocale::China));
calendar->setHorizontalHeaderFormat(QCalendarWidget::SingleLetterDayNames);
效果如下:
11、去掉QMainwindow的标题栏
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowFlags(Qt::FramelessWindowHint);//去掉标题栏
this->setGeometry(QRect(950, 55, 350, 250));//可设置窗口显示的方位与大小
//this->setWindowOpacity(0.7);//设置透明1-全体透明
//this->setAttribute(Qt::WA_TranslucentBackground, true);//设置透明2-窗体标题栏不透明,背景透明
this->resize(300,300);//显示大小
}
相关文章推荐
- QT 多线程程序设计
- 9.9 qt5creator + opencv2.4.6 的界面程序
- 【Qt编程】Qt学习之窗口间的相互切换
- 【Qt编程】Qt学习之窗口间的相互切换
- 9.10 QT VS2010 Opencv图像界面程序
- MQTT协议简记
- Qt下libusb-win32的使用方法
- Qt常用类及类方法简介之 QAction类
- ubuntu下配置qt+opengl+opencv
- QT 的信号与槽机制介绍(转)
- QT应用程序之国际化实例步骤及可能出错的细节
- Qt总结1——槽与信号
- Qt creator error: LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- Qt中Qstring,char,int,QByteArray之间到转换
- QT4.8.5+qt-vs-addin-1.1.11+VS2010安装配置和QT工程的新建和加载
- 关于QT配置ini文件的建立,读取,写入操作
- Qt入门-字符串类QString
- 【笔记】给Qt内嵌一个Chrome吧
- Qt同步方式进行http请求
- mysql.ZIP安装教程和配置QT5.2的mysql