QT4.6.2 学习问题摘录

QT4.6.2 学习问题摘录


1. QT重写窗口关闭事件

xxx.h

protected:
    void closeEvent(QCloseEvent *e);

xxx.cpp

void chatForm::closeEvent(QCloseEvent *e)
{
    //关闭资源,更新数据库信息等
    this->close();
}


2. liunx下qt create中编辑代码时输入奇慢无比

估计是和QDeveloper是有冲突的,把QDeveloper删除后,代码输

入变得流畅。


3. splitter窗口后使用gridlayout布局窗口实现splitter窗口自

动缩放大小。

设定各个窗口缩放比例:
    ui->splitterH->setStretchFactor(0, 0);
    ui->splitterH->setStretchFactor(1, 1);
    ui->splitterV->setStretchFactor(0, 1);
    ui->splitterV->setStretchFactor(1, 0);

 

4. gridlayout布局后设定边框大小

ui->gridLayout->setContentsMargins(2, 2, 2, 6);

 

5. 父窗口的变量值通过子窗口构造函数传递进去

chatForm::chatForm(QString id, QString peerIp, QString

peerPort, QUdpSocket *udpSocket):ui(new Ui::chatForm)
{
    this->id = id;
    this->serverIp = peerIp;
    this->serverPort = peerPort;
    this->udpSocket = udpSocket;
    ui->setupUi(this);
}

 

6. 设定按钮为透明

    ui->regButton->setFlat(true);

 

7. 对话框设定是否为模态对话框

        regdialog = new RegDialog();
        regdialog->setModal(true);
        //regdialog->setModal(false);//默认值
        regdialog->setWindowTitle("test");
        //regdialog->setWindowIcon();
        regdialog->show();

 

8. 设定支持中文显示

 

注意使用tr()函数

 

#include <QTextcodec>
    //support chinese character set
    QTextCodec::setCodecForCStrings

(QTextCodec::codecForName("GB2312"));
    QTextCodec::setCodecForLocale

(QTextCodec::codecForName("GB2312"));
    QTextCodec::setCodecForTr(QTextCodec::codecForName

("GB2312"));

 

9. 重写QSqlQueryModel中的函数自定义tableview显示方式

#include "mysqlquerymodel.h"
#include <QSqlQuery>

MySqlQueryModel::MySqlQueryModel()
{
}

QVariant MySqlQueryModel::data(const QModelIndex & index,

int role) const
{
    QVariant value = QSqlQueryModel::data(index, role);
   
    if (value.isValid() && role == Qt::DisplayRole &&

index.column() == 2)
    {
        value = (value.toInt() == 1 ? tr("在线") : tr("离

线"));
        return value;
    }

    else if (role == Qt::TextColorRole && index.column()

== 2)
    {
        return qVariantFromValue(QColor(Qt::red));
    }

    else return value;

}


10. 使用多线程处理TCP连接

在incomingConnection实现中开辟线程处理连接,

socketDescriptor作为线程构造函数的参数传入

//.h

#ifndef TCPSOCKSERVER_H
#define TCPSOCKSERVER_H

#include <QStringList>
#include <QTcpServer>

class TcpSockServer : public QTcpServer
{
    Q_OBJECT

public:
    TcpSockServer(QObject *parent = 0);

protected:
    void incomingConnection(int socketDescriptor);

private:

};

#endif // TCPSOCKSERVER_H


//.cpp

#include "tcpsockserver.h"
#include "tcpconthread.h"

TcpSockServer::TcpSockServer(QObject *parent)
     : QTcpServer(parent)
{
}


void TcpSockServer::incomingConnection(int

socketDescriptor)
{
    TcpConThread *thread = new TcpConThread

(socketDescriptor, this);
    connect(thread, SIGNAL(finished()), thread, SLOT

(deleteLater()));
    thread->start();
}

 

你可能感兴趣的:(thread,多线程,数据库,qt,character,Signal)