1、初始化界面
QPixmap pixmap(":/Pim/123.jpg");
QSplashScreen splash(pixmap);
splash.show();
{
//初始化
for(long i=1;i<1000000000;i++);
}
splash.finish(&w);//QSplashScreen::finish(QWidget *mainWin);
2、共享内存,图片
void MainWindow::on_pushButton_5_clicked()
{
QString fileName = QFileDialog::getOpenFileName(0, QString(), QString(),
tr("Images (*.png *.xpm *.jpg)"));
QImage image;
if (!image.load(fileName)) {
qDebug()<<"Selected file is not an image, please select another.";
return;
}
ui->label->setPixmap(QPixmap::fromImage(image));//
QBuffer buffer;
buffer.open(QBuffer::ReadWrite);
QDataStream out(&buffer);
out << image;//
int size = buffer.size();
sharedMemory= new QSharedMemory("memoryKey");//需要传入keyName,才可以
if(sharedMemory->isAttached()) {
if(!sharedMemory->detach()) {
QMessageBox::information(this, tr("错误"), tr("共享内存失败释放"), QMessageBox::Yes);
return;
}
}
//sharedMemory.setKey("memoryKey"); //更换关键字
if(!sharedMemory->create(buffer.size())) {
QMessageBox::information(this, tr("创建共享内存失败"), tr(sharedMemory->errorString().toLatin1().data()), QMessageBox::Yes);
return;
}
// QSharedMemory sharedMemory;
sharedMemory->lock();
char *to = (char*)sharedMemory->data();
const char *from = buffer.data().data();
memcpy(to, from, qMin(sharedMemory->size(), size));
sharedMemory->unlock();
}
void MainWindow::on_pushButton_6_clicked()
{
sharedMemory= new QSharedMemory("memoryKey");//需要传入keyName,才可以
if(!sharedMemory->attach()){
QMessageBox::information(this, tr("错误"), tr("共享内存打开失败"), QMessageBox::Yes);
return;
}
QBuffer buffer;
QDataStream in(&buffer);
QImage image;
sharedMemory->lock();
buffer.setData((char*)sharedMemory->constData(), sharedMemory->size());
buffer.open(QBuffer::ReadOnly);
in >> image;//read
sharedMemory->unlock();
sharedMemory->detach();
ui->label->setPixmap(QPixmap::fromImage(image));
}
3、鼠标位置
#include<QPushButton>
#include<QMouseEvent>
this->setMouseTracking(true);//MainWindow
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
event->ignore();
if ((windowType() != Qt::Widget)) {
event->accept();
QWidget* w;
while ((w = QApplication::activePopupWidget()) && w != this){
w->close();
if (QApplication::activePopupWidget() == w)
w->hide(); // hide at least
}
if (!rect().contains(event->pos())){
close();
}
}
ui->label_3->setText(QString("<center><h1>Move: (%1, %2)</h1></center>").arg(QString::number(event->x()),QString::number(event->y())));
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
ui->label_4->setText(QString("<center><h1>Press: (%1, %2)</h1></center>").arg(QString::number(event->x()),QString::number(event->y())));
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
QString msg;
msg.sprintf("<center><h1>Release: (%d, %d)</h1></center>",event->x(), event->y());
ui->label_5->setText(msg);
}
4、截图视频
录视频
//void MainWindow::on_pushButton_1_clicked()
//{
// if(ui->checkBox->isChecked())
// {
this->hide();
// this->timerp=new QTimer;
// connect(this->timerp,&QTimer::timeout,this,&MainWindow::shotScreenSlot);
// this->timerp->start(1000/24);
// }
// else{
// qApp->beep();
// }
//}
//void MainWindow::shotScreenSlot()
//{
// pixmapp= QPixmap::grabWindow(QApplication::desktop()->winId());
// ui->label->setPixmap(this->pixmapp.scaled(ui->label->size()));
this->show();
// // this->timerp->stop();
//}
//截图
void MainWindow::on_pushButton_1_clicked()
{
if(ui->checkBox->isChecked())
{
this->hide();
this->timerp=new QTimer;
connect(this->timerp,&QTimer::timeout,this,&MainWindow::shotScreenSlot);
this->timerp->start(ui->spinBox->value()*1000);
}
else{
qApp->beep();
}
}
#include<QDebug>
#include<QClipboard>
void MainWindow::shotScreenSlot()
{
pixmapp= QPixmap::grabWindow(QApplication::desktop()->winId());
QClipboard *clipboard = QGuiApplication::clipboard();
// QString originalText = clipboard->text();
// qDebug()<<originalText;
// clipboard->setImage(this->pixmapp);
ui->label->setPixmap(this->pixmapp.scaled(ui->label->size()));
this->show();
this->timerp->stop();//QTimer *timerp;
}
5.sqlit、mysql
//smysql
db=QSqlDatabase::addDatabase("QMYSQL");//QSQLITE
db.setHostName("localhost");
db.setPort(3306);
db.setUserName("root");
db.setPassword("smts");
db.setDatabaseName("student");
if(!db.open( ))
qDebug()<<"错误:"<<db.lastError();
else
qDebug()<<"成功:"<<db.lastError();
/*
//qsqlite #include<QVariant>
db=QSqlDatabase::addDatabase("QSQLITE");//QSQLITE
db.setDatabaseName("studb.db");
// db.setUserName("staff");
if(!db.open( ))
qDebug()<<"错误:"<<db.lastError();
else
qDebug()<<"成功";
QSqlQuery query;
query.prepare("INSERT INTO staff (id, name, age) "
"VALUES (:id, :name, :age)");
query.bindValue(":id", 1001);
query.bindValue(":name", "Bart");
query.bindValue(":age", "Simpson");
query.exec();
QString select_all_sql = "select * from staff";
query.prepare(select_all_sql);
if(!query.exec())
{
qDebug()<<query.lastError();
}
else
{
while(query.next())
{
int id = query.value(0).toInt();
QString name = query.value(1).toString();
int age = query.value(2).toInt();
qDebug()<<QString("id:%1 name:%2 age:%3").arg(id).arg(name).arg(age);
}
}
*/
// QList<QVariant> list = query.boundValues().values();
// for (int i = 0; i < list.size(); ++i)
// qDebug() << i << ": " << list.at(i).toString().toUtf8().data() << endl;
// while(query.next())
// {
// int rowid=query.value(0).toUInt();
// QString nameSqlite=query.value(1).toString();
// int age=query.value(2).toInt();
// qDebug()<<rowid<<" "<<nameSqlite<<" "<<age;
// }
// QSqlQuery query3;
// QString sqlcreate=QString("create table staff(id integer primary key autoincrement,"
// "name varchar(20),"
// "age int);");
// qDebug()<<query3.exec(sqlcreate);
// QString sqlcreate2=QString("insert into staff(name,age) values('助手1',20);");
// if(!query3.exec(sqlcreate2))
// {
// qDebug()<<query3.exec(sqlcreate2)<<db.lastError();
// }
// QString sqlcreate3=QString("select * from staff;");
// if(!query3.exec(sqlcreate3))
// {
// qDebug()<<query3.exec(sqlcreate3)<<db.lastError();
// }
// while(query3.next())
// {
// qDebug()<<query3.value("name").toString();
// qDebug()<<query3.value("age").toInt();
// }
// QString sqldelete=QString("delete from staff where id=4");
// if(!query3.exec(sqldelete))
// {
// qDebug()<<"delete data error"<<db.lastError();
// }
// sqldelete=QString("update staff set name = '历史' where id=3");
// if(!query3.exec(sqldelete))
// {
// qDebug()<<"delete data error"<<db.lastError();
// }
// sqlcreate3=QString("select * from staff;");
// if(!query3.exec(sqlcreate3))
// {
// qDebug()<<query3.exec(sqlcreate3)<<db.lastError();
// }
// while(query3.next())
// {
// qDebug()<<query3.value("name").toString();
// qDebug()<<query3.value("age").toInt();
// }
// QString sqlcreate=QString("INSERT INTO Websites (name, url, alexa, country)"
// "VALUES ('百度','https://www.baidu.com/','4','CN')");
// QString sqlcreate=QString("INSERT INTO staff(Name,Age,Gender)"
// "VALUES(:Name,:Age,:Gender)");
// query.prepare("INSERT INTO table_name (column1,column2,column3)"
// "VALUES (value1,value2,value3)");
// query.bindValue("value1","小米");
// query.bindValue("value2","12");
// query.bindValue("value3","13123213");
// query.exec();
// QSqlQuery query;
// query.prepare("INSERT INTO person (id, forename, surname) "
// "VALUES (:id, :forename, :surname)");
// query.bindValue(":id", 1001);
// query.bindValue(":forename", "Bart");
// query.bindValue(":surname", "Simpson");
// qDebug()<<query.exec();
// QSqlQuery query2;
// query2.exec("INSERT INTO employee (id, name, salary) "
// "VALUES (1001, 'Thad Beaumont', 65000)");
// qDebug()<<query2.exec();
// connect(ui->actionbug_H,&QAction::triggered,this,&MainWindow::close)
}