基本窗口
一、
1、QWidget是所有窗口部件的基类
几个相关的函数:
move(*) 移动窗体到某位置
setGeometry()设置窗体位置
setMaximumSize(400,400); 设置最大尺寸
setMinimumSize(300,300);
setGeometry函数是绝对定位,当窗口大小改变了也不会变,所以不太好
尝试:
#include "widget.h"
#include <QApplication>
#include<QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
//设置绝对坐标和大小,窗口最左上角
w.setGeometry(50,50,300,300);
qDebug()<<w.x()<<" "<<w.y();
qDebug()<<w.frameGeometry().width()<<" "<<w.geometry().width();
return a.exec();
}
输出结果:
41 12
318 300
首先是设置绝对坐标,这个绝对坐标是不包含标题栏的客户区
所以 w.x()是包不含标题栏的
而w.frameGeometry().width()是包含所有的区域,整个窗口部件的大小
w.geometry().width();是客户区的大小,不包含标题栏
2.标签QLabel
标签一般用于显示简单的文本
QLabel *label=new QLabel(this);
//设定位置
label->setGeometry(10,10,150,80);
//设置文字
label->setText(“Label”);
//设置文本对其方式。多个参数用“|”分割
label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
其对齐方式有如下几种
AlignTop 将文本添加到对象的上部
AlignButton 将文本添加到对象的下部
AlignLeft 沿着对象的左边添加文本
AlignRight 沿着对象的右边添加文本
AlignHCenter 将文本添加到对象水平中心的位置
AlignVCenter 将文本添加到对象的垂直中心位置
AlignCenter 将文本添加到对象的中心位置
可以使用标签显示位图:
#include "widget.h"
#include <QApplication>
#include<QDebug>
#include<QLabel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
QLabel *p=new QLabel(&w);
//创建img对象
QImage img= QImage("C:\\Users\\asus\\Desktop\\OpenCV\\badApple.jpg");
//使用标签显示像素图,但是需要借助QPixmap
p->setPixmap(QPixmap::fromImage(img));
p->setGeometry(0,0,img.width(),img.height());
w.show();
return a.exec();
}
3.QButton和QIcon
这两个比较简单,定义按钮和图标
然后调用Button的setIcon函数来绑定图标
但是如果想让button显示其包含的文字,可调用setFlat(true)函数设置为平面显示,这样文字就显示在了图标的旁边,而不是被图标覆盖
Dialog::Dialog(QWidget *parent) : QDialog(parent)
{
resize(150,150); //设置主窗体大小
pushButton1 = new QPushButton(this); //新建按钮
QIcon icon1(":/rc/play.ico"); //定义图标对象
pushButton1->setIcon(icon1); //设置按钮的图标
pushButton1->setGeometry(20, 20, 70, 40);
pushButton2 = new QPushButton(this);
QIcon icon2(":/rc/open.ico");
pushButton2->setIcon(icon2);
pushButton2->setFlat(true); //设置为平面显示,其实就是去掉边框,并且在旁边显示text
pushButton2->setText("Open"); //设置按钮文本信息
pushButton2->setGeometry(20, 70, 70, 40);
}
4.组合框QcomboBox(下拉框)
下面来尝试一下一个对话框中包含下拉框框的程序
首先将图片资源放到工程下
修改
Dialog.h文件
就是加个槽函数和下拉框指针
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include<QComboBox>
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog();
//定义槽函数
public slots:
void onChanged(int index);
private:
QComboBox*comBox;
};
#endif // DIALOG_H
修改cpp文件
创建对象,添加下拉条并绑定图片和文字
调整大小
然后定义槽函数
#include "dialog.h"
#include<QMessageBox>
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
comBox=new QComboBox(this);
comBox->setGeometry(20,20,200,100);
QIcon icon1(":/com/1.ico");
//下拉框设置第一个项目,并设置图标和文字
comBox->addItem(icon1,"this is one");
QIcon icon2(":/com/C:/Users/asus/Desktop/OpenCV/badApple.jpg");
//也可以只设置文字
comBox->addItem("this is two");
//然后将第1个item绑定一些属性
comBox->setItemIcon(1,icon2);
//连接信号槽
connect(comBox,SIGNAL(currentIndexChanged(int)),this,SLOT(onChanged(int)));
this->resize(200,200);
this->setWindowTitle("QComboBoxDemo");
}
//实现槽函数
void Dialog::onChanged(int index)
{
//当切换条的时候,切换到对应index会出发warning
//四个参数,分别指明父亲对象,弹出的信息框的标题,信息内容,和显示确定选项
QMessageBox::warning(this,"message",comBox->itemText(index),QMessageBox::Ok);
}
Dialog::~Dialog()
{
}
效果如下(注意路径不要写错)
5、QListWidget列表框
跟下拉框不一样,这个直接就是一个窗口,然后里面可以有很多选项
学习代码:
头文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QLabel>
#include<QListWidget>
class Widget : public QWidget
{
Q_OBJECT
private:
//标签指针和列表指针
QLabel*label;
QListWidget*list;
public:
Widget(QWidget *parent = 0);
~Widget();
};
#endif // WIDGET_H
cpp文件
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
label=new QLabel(this);
//设置标签宽度
label->setFixedWidth(70);
list=new QListWidget(this);
//可以传入图片参数,这里我就不传了
//tr只是可以让语言被翻译,更加国际化
list->addItem(new QListWidgetItem(tr("Line")));
list->addItem(new QListWidgetItem(tr("Rectangle")));
list->addItem(new QListWidgetItem(tr("Oval")));
list->addItem(new QListWidgetItem(tr("triangle")));
label->setGeometry(20,20,80,20);
list->setGeometry(20,50,200,100);
//绑定信号槽,当选的列表项目更改时,标签更改为选的文字
connect(list,SIGNAL(currentTextChanged(QString)),label,SLOT(setText(QString)));
}
Widget::~Widget()
{
}
6、单行编辑框QLineEdit
学习使用单行编辑框
这里自己尝试一下
头文件:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QLineEdit>
#include<QCheckBox>
#include<QLabel>
#include<QPushButton>
class Widget : public QWidget
{
Q_OBJECT
private:
QLineEdit*edit;
QCheckBox*read;
QCheckBox*pass;
QLabel*lab;
QPushButton *btn;
public:
Widget(QWidget *parent = 0);
~Widget();
public slots:
void on_clicked(bool x);
void on_clicked1(bool x);
void on_clicked2(bool x);
};
#endif // WIDGET_H
cpp文件
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
edit=new QLineEdit(this);
read=new QCheckBox(this);
pass=new QCheckBox(this);
read->setText("Read only");
pass->setText("hide");
edit->setText("text edit");
read->setGeometry(10,10,100,100);
pass->setGeometry(10,60,100,100);
pass->setText("加密");
read->setText("只读");
edit->setGeometry(10,120,400,30);
btn=new QPushButton(this);
connect(read,SIGNAL(clicked(bool)),this,SLOT(on_clicked(bool)));
connect(pass,SIGNAL(clicked(bool)),this,SLOT(on_clicked1(bool)));
lab=new QLabel(this);
lab->setGeometry(10,160,400,30);
btn->setText("input");
btn->setGeometry(100,10,100,100);
connect(btn,SIGNAL(clicked(bool)),this,SLOT(on_clicked2(bool)));
}
void Widget::on_clicked(bool x)
{
if(x)
{
//设置为只读
edit->setReadOnly(true);
}
else
{//取消设置只读
edit->setReadOnly(false);
}
}
void Widget::on_clicked1(bool x)
{
if(x)
{
//设置文本类型
edit->setEchoMode(QLineEdit::Password);
}
else
{
edit->setEchoMode(QLineEdit::Normal);
}
}
void Widget::on_clicked2(bool x)
{
//用标签显示文本框中的内容
lab->setText(edit->text());
}
Widget::~Widget()
{
}
7、滑动条QSlider