QT学习笔记(1)
QT学习笔记(1)
停靠窗口dock
#include "dockwin.h"
#include<QWidget>
#include<QMainWindow>
#include<QTextEdit>
#include<QDockWidget>
DockWin::DockWin(QWidget *parent):QMainWindow(parent)
{
resize(400,200);
setWindowTitle(tr("停靠窗口测试"));
//主窗口
QTextEdit *t0=new QTextEdit(this);
t0->setText("主窗口");
t0->setAlignment(Qt::AlignCenter);
setCentralWidget(t0);
//第一个浮动窗口
QDockWidget *dock1=new QDockWidget("停靠窗口1",this);
dock1->setAllowedAreas(Qt::LeftDockWidgetArea|Qt::RightDockWidgetArea);
QTextEdit *t1=new QTextEdit("可左右停靠");
dock1->setWidget(t1);
addDockWidget(Qt::RightDockWidgetArea,dock1);
//第二个浮动窗口
QDockWidget* dock2=new QDockWidget("停靠窗口2",this);
dock2->setFeatures(QDockWidget::DockWidgetClosable|QDockWidget::DockWidgetFloatable);
QTextEdit *t2=new QTextEdit("可关闭,可浮动");
dock2->setWidget(t2);
addDockWidget(Qt::RightDockWidgetArea,dock2);
//第三个浮动窗口
QDockWidget *dock3=new QDockWidget("停靠窗口3");
dock3->setFeatures(QDockWidget::NoDockWidgetFeatures);
QTextEdit *t3=new QTextEdit("禁用所有功能");
dock3->setWidget(t3);
addDockWidget(Qt::RightDockWidgetArea,dock3);
}
运行结果
布局splitter
#include<QApplication>
#include<QFont>
#include<QSplitter>
#include<QTextEdit>
int main(int argc,char*argv[])
{
QApplication a(argc,argv);
QFont font("ZYSong18030",18);
a.setFont(font);
//左侧QSplitter
QSplitter* left=new QSplitter(Qt::Horizontal,0);
QTextEdit* text1=new QTextEdit(QObject::tr("Left Widget"),left);
text1->setAlignment(Qt::AlignCenter);
//右侧QSplitter
QSplitter*right=new QSplitter(Qt::Vertical,left);
QTextEdit *text2=new QTextEdit(QObject::tr("Right Top"),right);
text2->setAlignment(Qt::AlignCenter);
QTextEdit *text3=new QTextEdit(QObject::tr("Right Middle"),right);
text3->setAlignment(Qt::AlignCenter);
QTextEdit *text4=new QTextEdit(QObject::tr("Right Bottom"),right);
text4->setAlignment(Qt::AlignCenter);
left->show();
return a.exec();
}
运行结果
菜单栏和工具栏
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QMenuBar>
#include<QMenu>
#include<QAction>
#include<QtDebug>
#include<QToolBar>
#include<QPushButton>
#include<QStatusBar>
#include<QLabel>
#include<QTextEdit>
#include<QDockWidget>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//菜单栏
QMenuBar* mbar=menuBar();
QMenu *mFile= mbar->addMenu("文件");
QMenu *mEdit= mbar->addMenu("编辑");
QAction *aNew= mFile->addAction("新建");
connect(aNew,QAction::triggered,
[=](){qDebug()<<"点击新建按钮"; }
);
mFile->addSeparator();
QAction *aOpen= mFile->addAction("打开");
connect(aOpen,QAction::triggered,
[=](){qDebug()<<"点击编辑按钮";}
);
//工具栏
QToolBar *tbar=addToolBar("tb");
tbar->addAction(aNew);
QPushButton *b=new QPushButton("(^-^)");
tbar->addWidget(b);
connect(b,QPushButton::clicked,
[=](){b->setText("o(╥﹏╥)o");}
);
//状态栏
QStatusBar*sbar=statusBar();
QLabel *l=new QLabel();
l->setText("状态栏");
sbar->addWidget(l);
sbar->addWidget(new QLabel("222"));
//从右往左添加addPermanentWidget
sbar->addPermanentWidget(new QLabel("333"));
//文本编辑器
QTextEdit *t=new QTextEdit(this);
setCentralWidget(t);
//浮动窗口
QDockWidget* dock=new QDockWidget();
addDockWidget(Qt::RightDockWidgetArea,dock);
QTextEdit *t2=new QTextEdit(this);
dock->setWidget(t2);
}
运行结果
模态对话框和非模态对话框
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QMenu>
#include<QMenuBar>
#include<QAction>
#include<QDialog>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QMenuBar *mbar=menuBar();
setMenuBar(mbar);
QMenu *m=mbar->addMenu("对话框");
QAction *ac1=m->addAction("模态对话框");
connect(ac1,QAction::triggered,
[=](){ QDialog *dia1=new QDialog(this);
dia1->resize(200,100);
dia1->exec();} );
QAction *ac2=m->addAction("非模态对话框");
connect(ac2,QAction::triggered,
[=](){ QDialog *dia2=new QDialog(this);
dia2->resize(200,100);
dia2->setAttribute(Qt::WA_DeleteOnClose);
dia2->show();} );
}
运行结果
我的计算器
#include "cal.h"
#include "ui_cal.h"
#include<QString>
Cal::Cal(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Cal)
{
ui->setupUi(this);
}
Cal::~Cal()
{
delete ui;
}
//通过按键获取输入
QString input="";
void Cal::on_pushButton1_clicked()
{
input+="1";
ui->label_display->setText(input);
}
void Cal::on_pushButton2_clicked()
{
input+="2";
ui->label_display->setText(input);
}
void Cal::on_pushButton3_clicked()
{
input+="3";
ui->label_display->setText(input);
}
void Cal::on_pushButton4_clicked()
{
input+="4";
ui->label_display->setText(input);
}
void Cal::on_pushButton5_clicked()
{
input+="5";
ui->label_display->setText(input);
}
void Cal::on_pushButton6_clicked()
{
input+="6";
ui->label_display->setText(input);
}
void Cal::on_pushButton7_clicked()
{
input+="7";
ui->label_display->setText(input);
}
void Cal::on_pushButton8_clicked()
{
input+="8";
ui->label_display->setText(input);
}
void Cal::on_pushButton9_clicked()
{
input+="9";
ui->label_display->setText(input);
}
void Cal::on_pushButton0_clicked()
{
input+="0";
ui->label_display->setText(input);
}
void Cal::on_pushButtonAdd_clicked()
{
input+="+";
ui->label_display->setText(input);
}
void Cal::on_pushButtonSub_clicked()
{
input+="-";
ui->label_display->setText(input);
}
void Cal::on_pushButtonMul_clicked()
{
input+="*";
ui->label_display->setText(input);
}
void Cal::on_pushButtonDiv_clicked()
{
input+="/";
ui->label_display->setText(input);
}
void Cal::on_pushButtonEqu_clicked()
{
Cal:: Calculate( input);
//input+="=";
ui->label_display->setText(input);
}
void Cal::on_pushButtonClear_clicked()
{
input="";
ui->label_display->setText(input);
}
//计算
#include<QStringList>
void Cal:: Calculate( QString &s)
{
int a,b;
QStringList list;
if(s.contains("+"))
{
list= s.split("+");
if(list.length()==2)
{
a=list[0].toInt();
b=list[1].toInt();
s.setNum(a+b);
}
else{ s="error";}
}
else if(s.contains("-"))
{
list= s.split("-");
if(list.length()==2)
{
a=list[0].toInt();
b=list[1].toInt();
s.setNum(a-b);
}
else { s="error"; }
}
else if(s.contains("*"))
{
list= s.split("*");
if(list.length()==2)
{
a=list[0].toInt();
b=list[1].toInt();
s.setNum(a*b);
}
else { s="error"; }
}
else if(s.contains("/"))
{
list= s.split("/");
if(list.length()==2)
{
a=list[0].toInt();
b=list[1].toInt();
if(b==0)
{
s="error";
return;
}
s.setNum(a/b);
}
else { s="error"; }
}
}
运行结果
表格测试
#include<QApplication>
#include<QWidget>
#include<QLabel>
#include<QTableWidget>
#include<QTableWidgetItem>
#include<QStringList>
#include<QDebug>
#include<QPushButton>
using namespace std;
class MyLabel:public QLabel
{
//Q_OBJECT
public slots://自定义槽函数
void resetText(QTableWidgetItem*item);
};
//槽函数的类外实现
void MyLabel::resetText(QTableWidgetItem *item)
{this->setText(item->text());}
//主函数
int main(int argc,char*argv[])
{
QApplication a(argc,argv);
QWidget w;
w.setWindowTitle("我的表格窗口");
w.resize(1000,500);
QTableWidget t(4,3,&w);
t.resize(1000,400);
t.setFont(QFont("宋体",20));
t.setHorizontalHeaderLabels(QStringList()<<"标题"<<"网址"<<"作者");
t.setEditTriggers(QAbstractItemView::NoEditTriggers);
t.setItem(0,0,new QTableWidgetItem("QT学习笔记(1)"));
t.setItem(0,1,new QTableWidgetItem("https://www.cnblogs.com/zhangdezhang/p/16277071.html"));
t.setItem(0,2,new QTableWidgetItem("zdz"));
t.setItem(1,0,new QTableWidgetItem("C++学习笔记(2)"));
t.setItem(1,1,new QTableWidgetItem("https://www.cnblogs.com/zhangdezhang/p/16268859.html"));
t.setItem(1,2,new QTableWidgetItem("zdz"));
t.setItem(2,0,new QTableWidgetItem("C++学习笔记(1)"));
t.setItem(2,1,new QTableWidgetItem("https://www.cnblogs.com/zhangdezhang/p/16264787.html"));
t.setItem(2,2,new QTableWidgetItem("zdz"));
MyLabel label;
label.setText("请选择一个单元格");
label.setParent(&w);
label.resize(900,150);
label.move( QPoint(0,400));
label.setAlignment(Qt::AlignCenter);
label.setFont(QFont("宋体",30));
QObject::connect(&t,QTableWidget::itemClicked,&label,MyLabel::resetText);
w.show();
return a.exec();
}
运行结果
按钮测试
#include<QApplication>
#include<QPushButton>
//按钮测试
int main(int argc,char*argv[])//主函数
{
QApplication a(argc,argv);//创建应用
QWidget w;//创建窗口
w.setWindowTitle("QT真是牛逼");//设置窗口标题
w.setStyleSheet("QWidget{background-color:green;}");//窗体背景绿色
QPushButton b("QPushButton按钮",&w);//设置父对象
b.move(100,100);//设置位置
//设置字体大小,设置背景颜色
b.setStyleSheet("QPushButton{font:20px;background-color:red;}");
b.resize(200,200);//设置尺寸
//设置信号槽,点击按钮--按钮隐藏
QObject::connect(&b,QPushButton::clicked,&b,QPushButton::hide);
w.show();//显示窗口
return a.exec();//循环等待
}
运行结果
QT思维导图
QT的信号槽的消息机制(事件/委托-事件处理函数);
QObject::connect(&b,QPushButton::clicked,&w,QWidget::close);