CMD Linux
ps -ef ดู procees
QLabel *textLabel = new QLabel(this);
ui->labelStatusDBColor->setStyleSheet("background-color: green");
QMessageBox msgBox;
QPushButton *connectButton = msgBox.addButton(tr("Connect"), QMessageBox::ActionRole);
QPushButton *abortButton = msgBox.addButton(QMessageBox::Abort);
QPushButton *abortButton2 = msgBox.addButton(QMessageBox::Ok);
msgBox.exec();
if (msgBox.clickedButton() == connectButton)
{
qDebug() << "connect";
}
else if (msgBox.clickedButton() == abortButton)
{
qDebug() << "abort";
}
else if (msgBox.clickedButton() == abortButton2)
{
qDebug() << "OK";
}
---------------------------------------------------------------------------
QMessageBox::information(this,"Wrong com port" ,"Please check comport");---------------------------------------------------------------------------QMessageBox msgBox;msgBox.setText("Le document a été modifié.");msgBox.setInformativeText("Voulez-vous enregistrer les changements ?");msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);msgBox.setDefaultButton(QMessageBox::Save);int ret = msgBox.exec();switch (ret) {case QMessageBox::Save:// Clic sur Enregistrerbreak;case QMessageBox::Discard:// Clic sur Ne pas enregistrerbreak;case QMessageBox::Cancel:// Clic sur Annulerbreak;default:// ne doit pas se produirebreak;}---------------------------------------------------------------#include <QApplication> #include <QMessageBox> #include <QDebug> // ... void MyWidget::someSlot() { QMessageBox::StandardButton reply; reply = QMessageBox::question(this, "Test", "Quit?", QMessageBox::Yes|QMessageBox::No); if (reply == QMessageBox::Yes) { qDebug() << "Yes was clicked"; QApplication::quit(); } else { qDebug() << "Yes was *not* clicked"; } }--------------------------------------------------QMessageBox msgBox;msgBox.setText("คุณต้องการจะทำรายการต่อไปใช่หรือไม่ ?");QPushButton *btnYes = msgBox.addButton(tr("Yes"), QMessageBox::ActionRole);QPushButton *btnNo = msgBox.addButton(tr("No"),QMessageBox::ActionRole);msgBox.exec();if (msgBox.clickedButton() == btnYes){qDebug() << "YES";}else if (msgBox.clickedButton() == btnNo){qDebug() << "NO";}---------------------------------------------------------------------------http://doc.qt.io/qt-4.8/qmessagebox.htmlhttp://qt.developpez.com/doc/4.7/qmessagebox/#icon-enum
========================================================================================
// .pro
QT += core gui sql network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = QtDB01
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
========================================================================================
// main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
// .h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QSqlQueryModel>
#include <QString>
#include <QtDebug>
#include <QtSql>
#include <QSqlQuery>
#include <QtGui>
#include <QtSql/QSqlQueryModel>
#include <QtCore>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_btnConnectDB_clicked();
void on_btnQry_clicked();
private:
Ui::MainWindow *ui;
QSqlQueryModel *model;
QSqlDatabase db;
};
#endif // MAINWINDOW_H
========================================================================================
// mainwindows.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
QString servername = "BIG\\SQLEXPRESS";
QString dbname = "test";
int cntData;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
db.setHostName(servername);
db.setDatabaseName(dbname);
QString connectionTemplate = "DRIVER={SQL SERVER};SERVER=%1;DATABASE=%2;";
QString connectionString = connectionTemplate.arg(servername).arg(dbname);
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName(connectionString);
db.open();
if (db.open())
{
qDebug() << "Open";
ui->TextEdit01->append("Open DB ");
}
else
{
qDebug() << "Error = " << db.lastError().text();
ui->TextEdit01->append(db.lastError().text());
}
this->model = new QSqlQueryModel();
}
MainWindow::~MainWindow()
{
db.close();
qDebug() << "Closing . . . ";
ui->TextEdit01->append("Closing...");
delete ui;
}
void MainWindow::on_btnConnectDB_clicked()
{
cntData++;
QString cnt = QString::number(cntData);
QString sqry = "INSERT INTO [dbo].[test_table]([firstname],[lastname]) VALUES (:firstname,:lastname)";
QSqlQuery qry;
qry.prepare(sqry);
qry.bindValue(":firstname","BIG"+cnt);
qry.bindValue(":lastname","BOM"+cnt);
if(qry.exec())
{
ui->TextEdit01->append("Record ");
}
}
void MainWindow::on_btnQry_clicked()
{
model->setQuery("SELECT [firstname],[lastname] FROM [dbo].[test_table]");
ui->tableView->setModel(model);
}
---------------------------------------------------------------------------------------
#Qry data from DB to tableViewhttps://doc.qt.io/qt-6/qsqlquery.htmlhttps://www.youtube.com/watch?v=3XE2bKUAxfw&list=PL2D1942A4688E9D63&index=52https://www.w3schools.com/sql/sql_where.asp