QT
来源:互联网 发布:正方形的风管知乎 编辑:程序博客网 时间:2024/05/17 06:24
QTimer定时器
如果需要在程序中周期性的处理事件,比如每秒种触发一次事件,那么可以使用QTimer。
QTime获取时间
Qdebug 输出问题的解决办法
QT5环境测试setWindowTitle的功能
会在屏幕的左上角显示标题
#include "mainwindow.h"#include <QApplication>int main(int argc, char *argv[]){ QApplication a(argc, argv); QWidget q; q.setWindowTitle("beautiful"); //设置图标 q.setWindowIcon(QIcon(":/prefix1(chinese)/4.jpg")); //设置主屏的大小为480px*270px q.resize(480,270); //设置主屏最大的宽度为480*270px q.setMaximumSize(480,270); //设置窗口的大小 this->setWindowTitle("apple"); q.show(); return a.exec();}
explicit
C++提供了关键字explicit,可以阻止不应该允许的经过转换构造函数进行的隐式转换的发生。声明为explicit的构造函数不能在隐式转换中使用。
pragma once
#pragma once是一个比较常用的C/C++杂注,只要在头文件的最开始加入这条杂注,就能够保证头文件只被编译一次。
/*对于某些类型,这一情况非常理想。但在大部分情况中,隐式转换却容易导致错误(不是语法错误,编译器不会报错)。隐式转换总是在我们没有察觉的情况下悄悄发生,除非有心所为,隐式转换常常是我们所不希望发生的。通过将构造函数声明为explicit(显式)的方式可以抑制隐式转换。也就是说,explicit构造函数必须显式调用。*/#include "1.h"CExample::CExample(void):m_iFirst(0){}CExample::~CExample(void){}CExample::CExample(int iFirst, int iSecond):m_iFirst(iFirst), m_iSecond(iSecond){}void f(String);String g(){ f(10); //错误:不能做隐式int->String转换 f("Arthur"); //可以:隐式转换,等价于f(String("Arthur")); return 10; //同上} int main(int argc,char* argv[]){CExample objOne; //调用没有参数的构造函数CExample objTwo(12, 12); //调用有两个参数的构造函数CExample objThree(12); //同上,可以传一个参数是因为该构造函数的第二个参数有默认值CExample objFour = 12; /*执行了隐式转换,等价于CExample temp(12);objFour(temp);注意这个地方调用了编译器为我们提供的默认拷贝构造函数*/String s1 = 'a'; //错误:不能做隐式char->String转换String s2(10); //可以:调用explicit String(int n);String s3 = String(10);//可以:调用explicit String(int n);再调用默认的复制构造函数String s4 = "Brian"; //可以:隐式转换调用String(const char *p);再调用默认的复制构造函数String s5("Fawlty"); //可以:正常调用String(const char *p);return 0;}
class CExample{ public: CExample(void); public: ~CExample(void); public: int m_iFirst; int m_iSecond; public: explicit CExample(int iFirst,int iSecond=4);};class String{ public: explicit String(int n); String(const char *p);};
void QWidget::resize ( const QSize & ) [槽]
设置不包含任何窗口框架的窗口部件的大小。详细情况请参考“size”属性。 void QWidget::resize ( int w, int h ) [虚 槽] 这是一个重载成员函数,提供了方便。它的行为基本上和上面的函数相同。 这个和resize( QSize(w, h) )一致。
void QWidget::setMaximumSize ( const QSize & )
设置这个窗口部件的最大大小。详细情况请参考“maximumSize”属性。void QWidget::setMaximumSize ( int maxw, int maxh ) [虚]这是一个重载成员函数,提供了方便。它的行为基本上和上面的函数相同。这个函数相当于setMaximumSize( QSize(maxw, maxh) )。设置最大宽度为maxw,最大高度为maxh。 eg:this->resize(QSize(480,270));
QT应用程序图标的设置
this->setWindowIcon(QIcon(“:/new/prefix1/img/icon.png”));
这里面做一个演示,将程序的图标设置成苹果的形状
#include "mainwindow.h"#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); this->setWindowIcon(QIcon(":/png/apple.png"));}MainWindow::~MainWindow(){ delete ui;}
除了可以使用正常的资源管理器的方式,添加资源外,还可以通过点击.src文件后右击的方式, 以普通文件的方式打开文件后进行编辑, 标签话语言比较容易理解 <RCC> <qresource prefix="/png"> <file>apple.png</file> <file>1.jpg</file> </qresource> </RCC>
Palatte
Qt中提供的调色板QPalette类就是专门用于管理控件的外观显示。QPalette类相当于对话框或控件的调色板,管理着控件和窗体的所有颜色。
每个窗体和控件都包含一个QPalette对象,在显示时,对其做相应的设置即可
设置控件颜色的方法是先调用QWidget::palette()获取当前面板,修改它为自定义的值后再通过方法QWidget::setPalette设置为新修改的面板。
在以下代码之前,必须先调用函数 setAutoFillBackground(true),设置窗体运行自动填充。常用的设置颜色方法如下:(1) void QPalette::setBrush ( ColorRole role, const QBrush & brush )改变所有组下指定角色role的画刷颜色值。(2) void QPalette::setBrush ( ColorGroup group, ColorRole role, const QBrush & brush )改变指定组group下的指定角色role的画刷颜色值。(3) void QPalette::setColor ( ColorRole role, const QColor & color )改变所有组下指定角色role的颜色值。(4) void QPalette::setColor ( ColorGroup group, ColorRole role, const QColor & color )改变指定组group下指定角色role的颜色值。
#ifndef PALETTE_H#define PALETTE_H#include <QDialog>#include <QComboBox>#include <QLabel>#include <QTextEdit>#include <QPushButton>#include <QLineEdit>class Palette : public QDialog{ Q_OBJECTpublic: Palette(QWidget *parent = 0); ~Palette(); void createCtrlFrame(); void createContentFrame(); void fillColorList(QComboBox *);private slots: void showWindow(); void showWindowText(); void showButton(); void showButtonText(); void showBase();private: QFrame *ctrlFrame; QLabel *windowLabel; QComboBox *windowComboBox; QLabel *windowTextLabel; QComboBox *windowTextComboBox; QLabel *buttonLabel; QComboBox *buttonComboBox; QLabel *buttonTextLabel; QComboBox *buttonTextComboBox; QLabel *baseLabel; QComboBox *baseComboBox; QFrame *contentFrame; QLabel *label1; QComboBox *comboBox1; QLabel *label2; QLineEdit *lineEdit2; QTextEdit *textEdit; QPushButton *okBtn; QPushButton *cancelBtn;};#endif // PALETTE_H
#include "palette.h"#include <QBoxLayout>Palette::Palette(QWidget *parent) : QDialog(parent){ createCtrlFrame(); createContentFrame(); QHBoxLayout *mainLayout = new QHBoxLayout(this); mainLayout->addWidget(ctrlFrame); mainLayout->addWidget(contentFrame);}void Palette::createCtrlFrame(){ ctrlFrame = new QFrame; //窗口背景色 windowLabel = new QLabel(tr("QPalette::window: ")); windowComboBox = new QComboBox; fillColorList(windowComboBox); connect(windowComboBox,SIGNAL(activated(int)),this,SLOT(showWindow())); windowTextLabel = new QLabel(tr("QPalette::WindowText: ")); //窗口前景色 windowTextComboBox = new QComboBox; fillColorList(windowTextComboBox); connect(windowTextComboBox,SIGNAL(activated(int)),this,SLOT(showWindowText())); buttonLabel = new QLabel(tr("QPalette::Button: ")); //窗口按钮的颜色 buttonComboBox = new QComboBox; fillColorList(buttonComboBox); connect(buttonComboBox,SIGNAL(activated(int)),this,SLOT(showButton())); buttonTextLabel = new QLabel(tr("QPalette::ButtonText: ")); //窗口按钮上面文本的颜色 buttonTextComboBox = new QComboBox; fillColorList(buttonTextComboBox); connect(buttonTextComboBox,SIGNAL(activated(int)),this,SLOT(showButtonText())); baseLabel = new QLabel(tr("QPalette::Base: ")); baseComboBox = new QComboBox; fillColorList(baseComboBox); connect(baseComboBox,SIGNAL(activated(int)),this,SLOT(showBase())); QGridLayout *mainLayout = new QGridLayout(ctrlFrame); mainLayout->setSpacing(20); mainLayout->addWidget(windowLabel,0,0); mainLayout->addWidget(windowComboBox,0,1); mainLayout->addWidget(windowTextLabel,1,0); mainLayout->addWidget(windowTextComboBox,1,1); mainLayout->addWidget(buttonLabel,2,0); mainLayout->addWidget(buttonComboBox,2,1); mainLayout->addWidget(buttonTextLabel,3,0); mainLayout->addWidget(buttonTextComboBox,3,1); mainLayout->addWidget(baseLabel,4,0); mainLayout->addWidget(baseComboBox,4,1);}void Palette::createContentFrame(){ contentFrame = new QFrame; label1 = new QLabel(tr("请选择一个值:")); comboBox1 = new QComboBox; label2 = new QLabel(tr("请输入字符串: ")); lineEdit2 = new QLineEdit; textEdit = new QTextEdit; QGridLayout *topLayout = new QGridLayout; topLayout->addWidget(label1,0,0); topLayout->addWidget(comboBox1,0,1); topLayout->addWidget(label2,1,0); topLayout->addWidget(lineEdit2,1,1); topLayout->addWidget(textEdit,2,0,1,2); okBtn = new QPushButton(tr("确认")); cancelBtn = new QPushButton(tr("取消")); QHBoxLayout *bottomLayout = new QHBoxLayout; bottomLayout->addStretch(1); bottomLayout->addWidget(okBtn); bottomLayout->addWidget(cancelBtn); QVBoxLayout *mainlayout = new QVBoxLayout(contentFrame); mainlayout->addLayout(topLayout); mainlayout->addLayout(bottomLayout); okBtn->setAutoFillBackground(true); //允许自动填充 cancelBtn->setAutoFillBackground(true); contentFrame->setAutoFillBackground(true);}void Palette::showWindow() //用于控制背景颜色的显示{ QStringList colorList = QColor::colorNames(); QColor color = QColor(colorList[windowComboBox->currentIndex()]); // contentFrame->setAutoFillBackground(true); QPalette p = contentFrame->palette(); p.setColor(QPalette::Window, color); contentFrame->setPalette(p); contentFrame->update();}void Palette::showWindowText() //对窗体的前景色进行设置{ QStringList colorList = QColor::colorNames(); QColor color = colorList[windowTextComboBox->currentIndex()]; QPalette p = contentFrame->palette(); p.setColor(QPalette::WindowText, color); contentFrame->setPalette(p);}void Palette::showButtonText(){ QStringList colorList = QColor::colorNames(); QColor color = QColor(colorList[buttonTextComboBox->currentIndex()]); QPalette p = contentFrame->palette(); p.setColor(QPalette::ButtonText , color); contentFrame->setPalette(p);}void Palette::showBase(){ QStringList colorList = QColor::colorNames(); QColor color = QColor(colorList[baseComboBox->currentIndex()]); QPalette p = contentFrame->palette(); p.setColor(QPalette::Base , color); contentFrame->setPalette(p);}void Palette::fillColorList(QComboBox *comboBox){ QStringList colorList = QColor::colorNames(); QString color; foreach (color, colorList) { QPixmap pix(QSize(70,20)); pix.fill(QColor(color)); comboBox->addItem(QIcon(pix), NULL); comboBox->setIconSize(QSize(70,20)); comboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents); }}void Palette::showButton(){ QStringList colorList = QColor::colorNames(); QColor color = QColor(colorList[buttonComboBox->currentIndex()]); //contentFrame->setAutoFillBackground(true); QPalette p = contentFrame->palette(); p.setColor(QPalette::Button , color); contentFrame->setPalette(p); contentFrame->update();}Palette::~Palette(){}
pixmap
的位图。
SetIcon
//图标将会被自动地放到按钮的上面,缺省时居中放置。如果图标太大,则会自动剪裁
setGeometry (9,9,50,25) 四个参数分别是指什么?
//从屏幕上(9,9)位置开始(即为最左上角的点),显示一个50*25的界面(宽50,高25)
setFlat(true);
//就是这句实现按钮透明的效果
QFONT
“
属性
QFont 类的常用属性如下所示: 字体的属性 获取所用成员函数 设置所有成员函数 名称 QString family() const; void setFamily(const QString &family); 点大小 int pointSize() const; void setPointSize(int pointSize); 像素大小 int pixelSize() const; void setPixelSize(int pixelSize); 粗细 int weight() const; void setWeight(int weight); 粗体 bool bold() const; void setBold(bool enable); 斜体 bool italic() const; void setItalic(bool enable); 下划线 bool underline() const; void setUnderline(bool enable);
其中设置粗体属性实际上就是将字体的粗细设为一个确定的值。点大小与像素大小是指定字体大小的两种方式。如果指定了点大小,则像素大小属性的值就是 -1;反之如果指定了像素大小,则点大小属性的值就是 -1。 如果指定的字体在使用时没有对应的字体文件,Qt 将自动选择最接近的字体。如果要显示的字符在字体中不 存在,则字符会被显示为一个空心方框。
QFont font; //设置字体类型 font.setFamily("Consolas"); //设置字体为粗体 font.setBold(true); //设置字体为斜体 font.setItalic(true); //设置字体有下划线 font.setUnderline(true); //设置字体大小 //1.像素 font.setPixelSize(13); //2.点 font.setPointSize(11); label=new QLabel(this); label->setText("游戏说明:贪吃蛇游戏可使用按钮或者w a s d控制蛇的走动"); 通过label调用此处的字体设置 label->setFont(font); label->setGeometry(QRect(10,10,400,50));
QMessageBox
QMessageBox::information(NULL, "Title", "Enter the game?", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
第一个参数parent,说明它的父组件;第二个参数title,也就是对话框的标题;第三个参数text,是对话框显示的内容;第四个参数buttons,声明对话框放置的按钮,默认是只放置一个OK按钮,这个参数可以使用或运算,例如我们希望有一个Yes和一个No的按钮,可以使用QMessageBox::Yes | QMessageBox::No,所有的按钮类型可以在QMessageBox声明的StandarButton枚举中找到;第五个参数defaultButton就是默认选中的按钮,默认值是NoButton,也就是哪个按钮都不选中
阅读全文
0 0
- QT
- QT
- QT
- Qt
- qt
- QT
- QT
- QT
- QT
- qt
- Qt
- QT
- qt
- qt
- Qt
- Qt
- QT
- qt
- Myeclipse 2017 安装与破解
- HDU 2054 A == B 【String 的骚操作 , string find 的应用】
- BZOJ 4397 Breed Counting
- 指针指向类的静态数据成员
- Maven命令的使用
- QT
- PYTHON基础笔记(3)
- c语言 走迷宫问题
- [BZOJ3924][ZJOI2015]幻想乡战略游戏-动态树分治
- 项目实践——一段时间的项目总结
- webpack配置相关知识详解
- 算法 一致性哈希算法
- excel数据分析--vlookup函数的使用
- 文章标题