您的位置:首页 > 编程语言 > Qt开发

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文件路径及前缀设置如图所示:



我的代码:


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);//显示大小

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: