杂记

从配置文件中读取信息
 
QVariantList getEffectdParameterListFromFile()
{
    QVariantList parameterList;
    QFile file(filePath);
    if (file.exists ())
    {
        if (file.open (QIODevice::ReadOnly))
        {
            QByteArray byte=file.readAll ();
            QJsonDocument byteJson = QJsonDocument::fromJson(byte);
            parameterList= byteJson.toVariant().toList();
        }
    }
    file.close ();
    return parameterList;
}
了一个验证器,确保字符串包含指定范围内的有效整数。
QValidator *validator = new QIntValidator(100, 999, this);
  QLineEdit *edit = new QLineEdit(this);
  // the edit lineedit will only accept integers between 100 and 999
  edit->setValidator(validator);
 
返回一个QLineEdit 输入框
QLineEdit * setEditStyle(QLineEdit *edit,const QString txt)
{
    edit->setPlaceholderText (txt);  // 占位符文本
    edit->setMaxLength (10);
    edit->setFont(QFont("utf8",10,QFont::Bold));  //字体
    ///圆角输入框
    edit->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px");
    //  设置限定器
    edit->setValidator(new QRegExpValidator(QRegExp("[a-zA-Z0-9]+$")));
    if (txt == "密码")
    {
        edit->setEchoMode(QLineEdit::Password);
    }
    return edit;
}
 
 
调用: QFormLayout *loginForm=new QFormLayout();
        userEdit=new QLineEdit(this);
    //  widget layout
    loginForm->addRow (setEditStyle(userEdit,"user"));
 
 
 
//  小部件布局   用图片作为按钮
  QFormLayout *loginBtnForm=new QFormLayout();
    loginBtn=new QPushButton(this);
    loginBtnForm->addRow (setBtnStyle (loginBtn,"登录"));
    quitBtn=new QPushButton(this);
    loginBtnForm->addRow (setBtnStyle (quitBtn,"退出"));
设置按钮的风格:
QPushButton *setBtnStyle(QPushButton *btn,const QString txt)
{
    QString borderImageName = "./style/btn.png";    文件路径
    QString sheet;
    QImage image(borderImageName);  //图片对象
    btn->setText (txt);
//             背景图片                                                        颜色
    sheet = "QPushButton{border-image:url(" + borderImageName + ");color: rgb(200, 255, 255);}";
   //                                                             按下改变颜色    
sheet+= "QPushButton:pressed{color: rgb(255, 255, 0);}";
    if (!image.isNull())
    {
        btn->setMinimumHeight(image.rect().height());
        btn->setMinimumWidth(image.rect().width());
  //设置风格
        btn->setStyleSheet(sheet);
    }
    return btn;
}

设置QTextEdit  风格

void  setTextEditStyle(QTextEdit *txtEdit)
{
    txtEdit->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px");  //设置边框
    txtEdit->setVerticalScrollBarPolicy (Qt::ScrollBarAlwaysOn); //滚动条
//    txtEdit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    txtEdit->verticalScrollBar()->setValue(txtEdit->verticalScrollBar()->maximumHeight()); //绑定高度
    txtEdit->setLineWrapMode(QTextEdit:: NoWrap);
   texEdit->setEnable(false);
    return;
}

设置背景风格

void setBackgroundStyle()
{
    QDesktopWidget* desktop = QApplication::desktop();
    move((desktop->width() - this->width())/2, (desktop->height() - this->height())/2);
    QString borderImageName = "./style/bg.png";
// 实例化一个image
    QImage image(borderImageName);
//实例一个调色板
    QPalette pal(this->palette());

    pal.setColor(QPalette::Background, Qt::black);
 //  
    pal.setBrush (this->backgroundRole(),QBrush(image.scaled(this->size())));
//自动填充  黑色
    this->setAutoFillBackground(true);
//设置调色板
    this->setPalette(pal);
    this->show();
}
 
启动另外一个程序
 QProcess p;
        p.execute (QString("killall a.out"));
        p.start("./a.out");
 
把map  存放到日志中
void  testLog(QVariantMap &map, QString fileName)
{
    // 把map   转化成json文件   
    QJsonDocument doc= QJsonDocument::fromVariant (QVariant(map));
// 转化为  string 
    QString str=doc.toJson ();
   log (fileName) << str ;
}
 
 
 
原文地址:https://www.cnblogs.com/countryboy666/p/11333276.html