mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ui_CreateEditTran.h"
#include "ui_SettingsDialog.h"
#include "ui_ListExercise.h"
#include <QTextCharFormat>
#include <QSqlDatabase>
#include <QtCore/QtDebug>
#include <QMessageBox>
#include <QSqlError>
#include <QTextCodec>
#include <QString>
#include <QSqlQuery>
#include <QPainter>
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
Ui_ListExercise uiListExerciseDialog;
QSqlDatabase MainWindow::ADD_DB (void)
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setHostName("bigblue");
db.setDatabaseName("flightdb");
db.setUserName("acarlson");
db.setPassword("1uTbSbAs");
return db;
}
void MainWindow::OPEN_DB(QSqlDatabase db)
{
if(!db.open())
{
QSqlError MyError=db.lastError();
QMessageBox msgBox;
msgBox.setText(codec->toUnicode("Ошибка при открытии базы данных "));
msgBox.setIcon(QMessageBox::Critical);
msgBox.setInformativeText(MyError.text());
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.exec();
}
}
QString IntToString(int numb)
{
char s[8];
sprintf(s,"%i",numb);
return s;
};
void MainWindow::LABEL_COLOR_CALENDAR(QDate getDate,int r, int g, int b, int a)
{
QTextCharFormat format;
QBrush brush;
QColor color;
// int r=200,g=145,b=234,a=255;
color.setRgb(r,g,b,a);
brush.setColor(color);
format.setBackground(brush);
ui->calendarWidget->setDateTextFormat(getDate,format);
}
QString MainWindow::CREATE_NAME_TABLE(QDate getDate)
{
QString NameTable;
NameTable.append(IntToString(getDate.day()));
NameTable.append("_");
NameTable.append(IntToString(getDate.month()));
NameTable.append("_");
NameTable.append(IntToString(getDate.year()));
return NameTable;
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::on_pushButton_clicked()
{
QSqlDatabase db = ADD_DB ();
OPEN_DB(db);
QDate getDate=ui->calendarWidget->selectedDate();
// qDebug() << getDate.day() <<getDate.month()<<getDate.year();
LABEL_COLOR_CALENDAR(getDate,200, 145,234,255);
QString NameTable=CREATE_NAME_TABLE(getDate);
qDebug() << NameTable;
QSqlQuery query;
QString str;
str="SELECT * FROM date_%1;";
str=str.arg(NameTable);
if (!query.exec(str))
{
str = "CREATE TABLE date_%1 ( "
"number INTEGER PRIMARY KEY NOT NULL, "
"exercise VARCHAR(128), "
"set1 VARCHAR(128), "
"set2 VARCHAR(128), "
"set3 VARCHAR(128), "
"set4 VARCHAR(128), "
"set5 VARCHAR(128), "
"set6 VARCHAR(128), "
"set7 VARCHAR(128), "
"set8 VARCHAR(128), "
"set9 VARCHAR(128), "
"set10 VARCHAR(128) "
");";
str = str.arg(NameTable);
qDebug() << str ;
if(!query.exec(str))
{
QSqlError MyError=db.lastError();
QString SError=MyError.text();
QMessageBox msgBox;
msgBox.setText(codec->toUnicode("Ошибка при создании таблицы "));
msgBox.setIcon(QMessageBox::Critical);
msgBox.setInformativeText(MyError.text());
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.exec();
}
}
// отрываем данную таблицу
str="SELECT * FROM date_%1;";
str=str.arg(NameTable);
if(!query.exec(str))
{
QSqlError MyError=db.lastError();
QString SError=MyError.text();
QMessageBox msgBox;
msgBox.setText(codec->toUnicode("Ошибка при открытии таблицы "));
msgBox.setIcon(QMessageBox::Critical);
msgBox.setInformativeText(MyError.text());
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.exec();
}
Ui_CreateEditTran uiDialog;
QDialog *dialog= new QDialog;
uiDialog.setupUi(dialog);
dialog->show();
int NumbColumnTable=uiDialog.NumbSet->value();
int NumbRowTable=uiDialog.NumbExercise->value();
uiDialog.tableWidget->setColumnCount(NumbColumnTable);
uiDialog.tableWidget->setRowCount(NumbRowTable);
QStringList StringHorizontalHeaderLabels;
QStringList StringVerticalHeaderLabels;
StringHorizontalHeaderLabels <<codec->toUnicode("Упражение");
int i=1;
while(i!=NumbColumnTable)
{
StringHorizontalHeaderLabels <<IntToString(i);
i++;
}
uiDialog.tableWidget->setHorizontalHeaderLabels(StringHorizontalHeaderLabels);
}
void MainWindow::on_pushButton_2_clicked()
{
QDate getDate=ui->calendarWidget->selectedDate();
LABEL_COLOR_CALENDAR(getDate,255, 255,255,255);
}
void MainWindow::on_Settings_clicked()
{
Ui_SettingsDialog uiDialog;
QDialog *dialog= new QDialog;
uiDialog.setupUi(dialog);
dialog->show();
//QBrush brush;
//QColor color;
//int r=200,g=145,b=234,a=120;
//color.setRgb(r,g,b,a);
//brush.setColor(color);
// QPalette p = uiDialog.tableWidget->palette();
////p.setBrush(QPalette::Normal,QPalette::Button,brush);
//p.setColor(QPalette::Normal,QPalette::Button,color);
// uiDialog.tableWidget->setPalette(p);
//
//// uiDialog.tableWidget->setBackgroundRole();
QPainter painter(dialog);
painter.setRenderHint(QPainter::Antialiasing,true);
painter.setPen(QPen(Qt::black,15,Qt::DashDotDotLine,Qt::RoundCap));
painter.setBrush(QBrush(Qt::green,Qt::SolidPattern));
painter.drawEllipse(80,80,400,240);
}
void MainWindow::on_ListExercise_clicked()
{
QDialog *dialog= new QDialog;
uiListExerciseDialog.setupUi(dialog);
dialog->show();
int NumbColumnTable=1;
int NumbRowTable=1;
uiListExerciseDialog.tableWidget->setColumnCount(NumbColumnTable);
uiListExerciseDialog.tableWidget->setRowCount(NumbRowTable);
QStringList StringHorizontalHeaderLabels;
StringHorizontalHeaderLabels <<codec->toUnicode("Список упражений");
uiListExerciseDialog.tableWidget->setHorizontalHeaderLabels(StringHorizontalHeaderLabels);
// растягиваем столбец по всей ширине таблицы
QSize size=uiListExerciseDialog.tableWidget->size();
uiListExerciseDialog.tableWidget->setColumnWidth(0,size.width());
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QSqlDatabase>
#include <QDate>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
QSqlDatabase ADD_DB (void);
void OPEN_DB(QSqlDatabase db);
void LABEL_COLOR_CALENDAR(QDate getDate,int r, int g, int b, int a);
QString CREATE_NAME_TABLE(QDate getDate);
protected:
void changeEvent(QEvent *e);
private:
Ui::MainWindow *ui;
private slots:
void on_ListExercise_clicked();
void on_Settings_clicked();
void on_pushButton_2_clicked();
void on_pushButton_clicked();
};
#endif // MAINWINDOW_H
ПС:извините за большой текст, мож выложить в другом месте... в споллере тоже хреново выглядит