Готови решения > C/C++/Qt/etc
Qt 4.7.4 C++ Ел.Библиотека и Търсачка
haccac:
Това приложение служи като електронна библиотека т.е. имате 20 или 30 текстови документа и искате да намерите с натискането на един бутон дума или израз във кои документи съществува и на кои места във тях.
нормално е да има и някой бъг но за сега не знам как да се справя с тях. В момент в който променя нещо ще го добавям.
lib.pro
--- Код: C++ (QT) ---#-------------------------------------------------## Project created by QtCreator 2011-03-26T18:30:59##------------------------------------------------- QT += core gui TARGET = Search_LibTEMPLATE = app RC_FILE = myapp.rs SOURCES += main.cpp\ mainwindow.cpp \ op_wr.cpp HEADERS += mainwindow.h \ op_wr.h FORMS += mainwindow.ui RESOURCES += \ res.qrc mainwindow.h #ifndef MAINWINDOW_H#define MAINWINDOW_H #include <QMainWindow>#include <QMessageBox>#include <QFontComboBox>#include <QComboBox>#include <QLineEdit>#include <QPushButton>#include <QDir>#include <QFile>#include <QFileDialog>#include <QFileInfo>#include <QTextStream>#include <QString>#include <QStringList>#include <QHash>#include <QProgressBar>#include <QListWidgetItem>#include <QTextCodec> namespace Ui { class MainWindow;} class MainWindow : public QMainWindow{ Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private: Ui::MainWindow *ui; QFontComboBox *font; QComboBox *cb_size; QLineEdit *ln_find; QPushButton *find_btn; //File Open,Save and SaveAs QString fl_saveAs; QString fl_save; //Library int err; QDir dir; QHash<int,QString> file_path; QHash<int,QString> file_name; QHash<int,QString> file_ext; int file_max; bool isFirstTime; bool isAcFind; QProgressBar *pr_bar; QString lib_dir; private slots: //Options Save void on_actionAdd_Library_triggered(); //Library void find_Button(); void on_listWidget_currentRowChanged(int currentRow); void getRefresh(); void getFiles(); void findInText(int number); void readFile(int number); void on_actionList_Editor_triggered(); void on_actionLybrary_triggered(); void on_actionRefresh_triggered(); void cb_current_size_Changed(); void on_actionText_underline_triggered(bool checked); void on_actionText_italic_triggered(bool checked); void on_actionText_bold_triggered(bool checked); void on_actionFormat_right_triggered(); void on_actionFormat_center_triggered(); void on_actionFormat_left_triggered(); void on_actionFormat_all_triggered(); void on_actionSaveAs_triggered(); void on_actionSave_triggered(); void on_actionOpen_triggered();}; #endif // MAINWINDOW_H
mainwindow.cpp
--- Код: C++ (QT) ---#include "mainwindow.h"#include "ui_mainwindow.h"#include "op_wr.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); //ComboBox Font font = new QFontComboBox; connect(font,SIGNAL(currentFontChanged(QFont)),ui->textEdit,SLOT(setCurrentFont(QFont))); ui->FormatBar->addWidget(font); //ComboBox Font Size cb_size = new QComboBox; for(int i = 8; i < 36; i++) { if(i > 12) { if(i%2 == 0) cb_size->addItem(QString::number(i)); } else { cb_size->addItem(QString::number(i)); } } connect(cb_size,SIGNAL(currentIndexChanged(int)),this,SLOT(cb_current_size_Changed())); ui->FormatBar->addWidget(cb_size); ln_find = new QLineEdit; ui->BooksBar->addWidget(ln_find); find_btn = new QPushButton(QIcon(":/books/res/Find.bmp")," Find"); find_btn->setFont(QFont("Times", 8, QFont::Bold)); ui->BooksBar->addWidget(find_btn); isFirstTime = true; isAcFind = false; ui->actionLybrary->setVisible(false); ui->progressBar->setVisible(false); ui->progressBar_2->setVisible(false); ui->progressBar->setValue(0); ui->progressBar_2->setValue(0); ui->lb_prBar->setVisible(false); ui->lb_prBar2->setVisible(false); getRefresh(); connect(find_btn, SIGNAL(pressed()), this, SLOT(find_Button()));} MainWindow::~MainWindow(){ delete ui;} //File void MainWindow::on_actionOpen_triggered(){ if(fl_saveAs.isEmpty()) fl_saveAs = QDir::currentPath() + "\\Library"; QString filename = QFileDialog::getOpenFileName( this, tr("Open Document"), fl_saveAs, tr("Document files (*.doc *.txt);;All files (*.*)")); if( !filename.isNull() ) { QFileInfo fl; fl.setFile(filename); fl_saveAs = fl.absolutePath(); fl_save = filename; QFile mFile(filename); if(!mFile.open(QFile::ReadOnly | QFile::Text)) { QMainWindow::close(); } QTextStream in(&mFile); ui->textEdit->setText(in.readAll()); } on_actionList_Editor_triggered();} void MainWindow::on_actionSave_triggered(){ if(fl_save.isEmpty()) fl_save = QDir::currentPath() + "\\Library"; QString filename = QFileDialog::getSaveFileName( this, tr("Save Document"), fl_save, tr("Documents (*.doc)")); if( !filename.isNull() ) { QFile mFile(filename); if(!mFile.open(QFile::WriteOnly | QFile::Text)) { QMainWindow::close(); } QTextStream out(&mFile); out << ui->textEdit->QTextEdit::toPlainText(); mFile.flush(); mFile.close(); }} void MainWindow::on_actionSaveAs_triggered(){ if(fl_saveAs.isEmpty()) fl_saveAs = QDir::currentPath() + "\\Library"; QString filename = QFileDialog::getSaveFileName( this, tr("Save Document"), fl_saveAs, tr("Documents (*.doc)")); if( !filename.isNull() ) { QFile mFile(filename); if(!mFile.open(QFile::WriteOnly | QFile::Text)) { QMainWindow::close(); } QTextStream out(&mFile); out << ui->textEdit->QTextEdit::toPlainText(); mFile.flush(); mFile.close(); }} //FormatBar void MainWindow::on_actionFormat_all_triggered(){ ui->textEdit->setAlignment(Qt::AlignJustify);} void MainWindow::on_actionFormat_left_triggered(){ ui->textEdit->setAlignment(Qt::AlignLeft);} void MainWindow::on_actionFormat_center_triggered(){ ui->textEdit->setAlignment(Qt::AlignCenter);} void MainWindow::on_actionFormat_right_triggered(){ ui->textEdit->setAlignment(Qt::AlignRight);} void MainWindow::on_actionText_bold_triggered(bool checked){ if(checked == true) { ui->textEdit->setFontWeight(99); } else { ui->textEdit->setFontWeight(0); }} void MainWindow::on_actionText_italic_triggered(bool checked){ if(checked == true) { ui->textEdit->setFontItalic(true); } else { ui->textEdit->setFontItalic(false); }} void MainWindow::on_actionText_underline_triggered(bool checked){ if(checked == true) { ui->textEdit->setFontUnderline(true); } else { ui->textEdit->setFontUnderline(false); }} void MainWindow::cb_current_size_Changed(){ int item = cb_size->currentText().toInt(); ui->textEdit->setFontPointSize(item);} //Books Bar void MainWindow::on_actionRefresh_triggered(){ ln_find->setText(""); find_Button();} void MainWindow::on_actionLybrary_triggered(){ ui->actionLybrary->setVisible(false); ui->actionList_Editor->setVisible(true); ui->actionRefresh->setVisible(true); ui->listWidget->setVisible(true); ui->lb_lib->setVisible(true); } void MainWindow::on_actionList_Editor_triggered(){ ui->actionList_Editor->setVisible(false); ui->actionLybrary->setVisible(true); ui->actionRefresh->setVisible(false); ui->listWidget->setVisible(false); ui->lb_lib->setVisible(false); ui->progressBar->setVisible(false); ui->progressBar_2->setVisible(false); ui->lb_prBar->setVisible(false); ui->lb_prBar2->setVisible(false);} //Library void MainWindow::getRefresh(){ ui->listWidget->clear(); ui->textEdit->clear(); getFiles(); ui->progressBar->setMaximum(file_max); foreach(int n,file_name.keys()) { ui->listWidget->addItem(QString::number(n + 1) + " - " + file_name[n] + "." + file_ext[n]); ui->listWidget->item(n)->setBackground(Qt::darkYellow); }} void MainWindow::getFiles(){ dir = QDir::currentPath() + "//Library"; int i = 0; foreach(QFileInfo fl, dir.entryInfoList()) { if(fl.isFile() && (fl.suffix() == "txt" || fl.suffix() == "doc" || fl.suffix() == "html")) { file_path.insert(i, fl.absoluteFilePath()); file_name.insert(i, fl.baseName()); file_ext.insert(i, fl.suffix()); i++; file_max = i; } }} void MainWindow::readFile(int number){ QString filename = file_path[number]; if( !filename.isNull() ) { QFile file(filename); if (!file.open(QFile::ReadOnly)) QMainWindow::close(); QString str; QByteArray data; ui->progressBar_2->setMaximum(file.size()/1024); while (!file.atEnd()) { data += file.readLine(); ui->progressBar_2->setValue(file.pos()/1024); } QTextCodec *codec = Qt::codecForHtml(data); str = codec->toUnicode(data); if (Qt::mightBeRichText(str)) { ui->textEdit->setHtml(str); } else { str = QString::fromLocal8Bit(data); ui->textEdit->setPlainText(str); } }} void MainWindow::findInText(int number){ QString searchString = ln_find->text(); QTextDocument *document = ui->textEdit->document(); bool found = false; if (isFirstTime == false) document->undo(); if(document->isEmpty()) { ui->listWidget->item(number)->setBackgroundColor(Qt::yellow); } else { ui->listWidget->item(number)->setBackgroundColor(Qt::darkYellow); } if (searchString.isEmpty()) { } else { QTextCursor highlightCursor(document); QTextCursor cursor(document); cursor.beginEditBlock(); QTextCharFormat plainFormat(highlightCursor.charFormat()); QTextCharFormat colorFormat = plainFormat; colorFormat.setForeground(Qt::red); while (!highlightCursor.isNull() && !highlightCursor.atEnd()) { highlightCursor = document->find(searchString, highlightCursor, QTextDocument::FindWholeWords); if(!highlightCursor.isNull()) { found = true; highlightCursor.movePosition(QTextCursor::WordRight,QTextCursor::KeepAnchor); highlightCursor.mergeCharFormat(colorFormat); } } cursor.endEditBlock(); isFirstTime = false; if (found == false) { ui->listWidget->item(number)->setBackgroundColor(Qt::red); } else { ui->listWidget->item(number)->setBackgroundColor(Qt::green); } if(document->isEmpty()) { ui->listWidget->item(number)->setBackgroundColor(Qt::yellow); } }} void MainWindow::on_listWidget_currentRowChanged(int currentRow){ if(isAcFind == true) { readFile(currentRow); findInText(currentRow); ui->progressBar->setValue(currentRow + 1); }} void MainWindow::find_Button(){ if(ui->listWidget->isVisible()) { isAcFind = true; ui->progressBar->setVisible(true); ui->progressBar_2->setVisible(true); ui->lb_prBar->setVisible(true); ui->lb_prBar2->setVisible(true); for(int i = 0; i < file_max; i++) { on_listWidget_currentRowChanged(i); } ui->progressBar->setVisible(false); ui->progressBar_2->setVisible(false); ui->lb_prBar->setVisible(false); ui->lb_prBar2->setVisible(false); } else { isAcFind = false; findInText(0); }} void MainWindow::on_actionAdd_Library_triggered(){ Op_WR op; op.op_Read(); op.op_list_text[0] = QFileDialog::getExistingDirectory(this, tr("Open Directory"), QDir::currentPath() + "\\Library\\", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); op.op_Write();}
op_wr.h
--- Код: C++ (QT) ---#ifndef OP_WR_H#define OP_WR_H #include <QStringList> class Op_WR{public: Op_WR(); QStringList op_list_text; int err; void op_Read(); void op_Write();}; #endif // OP_WR_H
op_wr.cpp
--- Код: C++ (QT) ---#include "op_wr.h"#include <QDir>#include <QFile>#include <QString>#include <QTextStream> Op_WR::Op_WR(){}//Optionsvoid Op_WR::op_Read(){ QString filename = QDir::currentPath().append("/op/op.dat"); if( !filename.isNull() ) { QFile mFile(filename); if(!mFile.open(QFile::ReadOnly | QFile::Text)) { err = 2; return; } QTextStream in(&mFile); op_list_text = in.readAll().split("\n"); }} void Op_WR::op_Write(){ QString filename = QDir::currentPath().append("/op/op.dat"); if( !filename.isNull() ) { QFile mFile(filename); if(!mFile.open(QFile::WriteOnly | QFile::Text)) { err = 2; return; } QTextStream out(&mFile); QString op_text = op_list_text.join("\n"); out << op_text; mFile.flush(); mFile.close(); }}
main.cpp
--- Код: C++ (QT) ---#include <QtGui/QApplication>#include "mainwindow.h" int main(int argc, char *argv[]){ QApplication a(argc, argv); MainWindow w; w.show(); return a.exec();} mainwindow.ui // Прикачен фаил с изображение
не забравяйте да създадете папка library и op.dat файл
echeveria:
Коментарите ти са адски оскдни - препоръчвам поне долу да дадеш малко повече инфо за това кое какво е щото от заглавието нищо не се разбира - и за другите е така! Гуугъл пък изобщо няма да разбере за какво става въпрос!
haccac:
Не съм правил коментари нямах намерение да го слагам. Понеже направихте нещо добро за сайта затова го поставих. Ако не се разбира кода да обясня всичко, или отгоре отгоре? Но ако искате подробно няма да е днес а вероятно утре.
wuser:
Е то не е проблем, ако някой нещо не е разбрал да попита. А аз съм във възторг, че пишеш точно за Кът. За съжаление на български няма почти нищо, даже при Нокия.
echeveria:
Не бе не какво е като част кода а за какво служи и какво предлага! Просто ианче не е нужно на никой ако не знае за какво е кода поне!
Навигация
[0] Списък на темите
Премини на пълна версия