• QT4编程过程中遇到的问题及解决办法


    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的标题栏

    1. MainWindow::MainWindow(QWidget *parent) :  
    2.     QMainWindow(parent),  
    3.     ui(new Ui::MainWindow)  
    4. {  
    5.     ui->setupUi(this);  
    6.     this->setWindowFlags(Qt::FramelessWindowHint);//去掉标题栏  
    7.     this->setGeometry(QRect(950, 55, 350, 250));//可设置窗口显示的方位与大小  
    8.     //this->setWindowOpacity(0.7);//设置透明1-全体透明  
    9.     //this->setAttribute(Qt::WA_TranslucentBackground, true);//设置透明2-窗体标题栏不透明,背景透明  
    10.     this->resize(300,300);//显示大小  
  • 相关阅读:
    CSS3阴影 box-shadow的使用和技巧总结
    css实现一行文字居中,多行文字左对齐
    让一个div里面的其它div或者标签内容居中
    巧用css内容生成
    Emmet-前端开发神器
    新手向:Vue 2.0 的建议学习顺序
    require() 源码解读
    Vue-webpack项目配置详解
    webpack---webpack构建vue多页面框架(一、工程布局)
    STS创建spring boot项目,pom.xml文件第一行报错
  • 原文地址:https://www.cnblogs.com/cy568searchx/p/3602220.html
Copyright © 2020-2023  润新知