⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 application.cpp~

📁 几个qt-linux的练习小程序
💻 CPP~
字号:
#include "application.h"
// #include "fontdialog.h"

#include <qimage.h>
#include <qpixmap.h>
#include <qtoolbar.h>
#include <qtoolbutton.h>
#include <qpopupmenu.h>
#include <qmenubar.h>
#include <qtextedit.h>
#include <qfile.h>
#include <qfiledialog.h>
#include <qstatusbar.h>
#include <qmessagebox.h>
#include <qapplication.h>
#include <qaccel.h>
#include <qtextstream.h>
#include <qpainter.h>
#include <qpaintdevicemetrics.h>
#include <qwhatsthis.h>
#include <qfont.h>
#include <qfontdialog.h>
#include <qcolordialog.h>

#include "filesave.xpm"
#include "fileopen.xpm"


window::window()
    : QMainWindow( 0, "", WPaintDesktop)
{
    createwindow();
    resize( 450, 600 );
}

void window::createwindow()
{
    setCaption("Qt");

    QPixmap openIcon, saveIcon;

    QToolBar * fileTools = new QToolBar( this, "file operations" );
    fileTools->setLabel( "File Operations" );

    openIcon = QPixmap( fileopen );
    
    QToolButton * fileOpen = new QToolButton( openIcon, "Open File", QString::null, this, SLOT(open()), fileTools, "open file" );

    saveIcon = QPixmap( filesave );
    QToolButton * fileSave = new QToolButton( saveIcon, "Save File", QString::null, this, SLOT(save()), fileTools, "save file" );


/*what  this*/
    (void)QWhatsThis::whatsThisButton( fileTools );
    
    const char * fileOpenText = "<p><img source=\"fileopen\"> " "Click this button to open a <em>new file</em>.<br>"
                 "You can also select the <b>Open</b> command "
                 "from the <b>File</b> menu.</p>";

    QWhatsThis::add( fileOpen, fileOpenText );

    // QMimeSourceFactory::defaultFactory()->setPixmap( "fileopen", openIcon);

    const char * fileSaveText = "<p>Click this button to save the file you "
                 "are editing. You will be prompted for a file name.\n"
                 "You can also select the <b>Save</b> command "
                 "from the <b>File</b> menu.</p>";

    QWhatsThis::add( fileSave, fileSaveText );

//File选项
    QPopupMenu * file = new QPopupMenu( this );//菜单栏
    menuBar()->insertItem( "&File", file );//工具栏

    file->insertItem( "&New", this, SLOT(newDoc()), CTRL+Key_N ); // 添加new 选项 并且加上快捷方式和关联槽函数

    int id;//记录添加工具栏选项后返回的选项编号
    id = file->insertItem( "&Open...", this, SLOT(open()), CTRL+Key_O );
    file->setWhatsThis( id, fileOpenText );//将帮助和选项关联

    id = file->insertItem("&Save", this, SLOT(save()), CTRL+Key_S );
    file->setWhatsThis( id, fileSaveText );

    id = file->insertItem( "Save &As...", this, SLOT(saveAs()));
    file->setWhatsThis( id, fileSaveText );

    file->insertSeparator();//分隔栏

    file->insertItem( "&Close", this, SLOT(close()), CTRL+Key_W );

    file->insertItem( "&Quit", qApp, SLOT( closeAllWindows() ), CTRL+Key_Q );

    menuBar()->insertSeparator();

//edit选项
    QPopupMenu *editMenu = new QPopupMenu(this);
    menuBar()->insertItem( "&EditMenu", editMenu );
    editMenu->insertItem( "&Copy", this, SLOT(copy()), CTRL+Key_C );
    editMenu->insertItem( "&Paste", this, SLOT(paste()), CTRL+Key_P );
    editMenu->insertItem( "&cUt", this, SLOT(cut()), CTRL+Key_U );
    editMenu->insertItem( "&Total", this, SLOT(total()), CTRL+Key_T );
    
    editMenu->insertItem( "&cLear", this, SLOT(clear()), CTRL+Key_L );

//other选项
    QPopupMenu *other = new QPopupMenu(this);
    menuBar()->insertItem( "&Other", other );
    other->insertItem( "&Font", this, SLOT(font()), CTRL+Key_N );
    other->insertItem( "&Color", this, SLOT(color()), CTRL+Key_R );


//Help选项
    QPopupMenu *help = new QPopupMenu(this);
    menuBar()->insertItem("&Help", help);

    

    // help->insertItem( "&About", this, SLOT(about()), Key_F1 );
    help->insertItem( "About &Qt", this, SLOT(aboutQt()));
    help->insertSeparator();
    help->insertItem( "What's &This", this, SLOT(whatsThis()), SHIFT+Key_F1 );

    e = new QTextEdit( this, "editor" );
    e->setFocus();
    setCentralWidget( e );//将e设置为中心
    statusBar()->message( "Ready", 2000 );
}

/*创建一个新的窗口*/
void window::newDoc()
{
    window *ed = new window;
    ed->setCaption("QT");
    ed->show();
}
/*获得要打开的文件*/
void window::open()
{
     QString fn = QFileDialog::getOpenFileName(
                    "/home",
                    "Images (*.txt *.xpm *.jpg)",
                    this,
                    "open file dialog",
                    "Choose a file to open" );
     load(fn);
     fn =filename;

}
/*显示文件*/
void window::load( const QString &fileName )
{
    QFile f( fileName );
    if ( !f.open( IO_ReadOnly ) )
    return;
    QTextStream ts( &f );
    e->setText( ts.read() );
    e->setModified( FALSE );
    setCaption(fileName);
}

/*保存*/
void window::save()
{   
    QString text = e->text();
//    QFile f(filename);
//    f.setName(filename);
    QFile f(caption());
    if ( !f.open( IO_WriteOnly ) ) {
    return;
    }
    QTextStream t( &f );
    t << text;
    f.close();

}

/*另存为*/

void window::saveAs()
{
     QString text = e->text();
     QString fn = QFileDialog::getSaveFileName(
                    "/home",
                    "Images (*.txt *.xpm *.jpg)",
                    this,
                    "save file dialog",
                    "Choose a file to save" );
    QFile f(fn);
    if ( !f.open( IO_WriteOnly ) ) {
    return;
    }
    QTextStream t( &f );
    t << text;
    f.close();
	
}

/*copy*/

void window::copy()
{
	e->copy();
}

/*paste*/
void window::paste()
{
	e->paste();
}

/* total*/
void window:: total()
{
	e->selectAll();
}

/* clear*/
void window:: clear()
{
	e->clear();
}

/* cut*/
void window:: cut()
{
	e->cut();
}

/* color*/
void window:: color()
{
	e->setColor(QColorDialog::getColor());
}
/* font*/
void window:: font()
{	
	e->setCurrentFont(QFontDialog::getFont(false));
/*	bool ok;
	QFont font = QFontDialog::getFont(&ok, QFont( "Helvetica [Cronyx]", 10 ), this );
	if( ok ){
//	e->setCurrentFont(font);
	}else{
	e->setCurrentFont(font);
	}
*/
}

/*关闭*/
void window::closeEvent( QCloseEvent* ce )
{
    if ( !e->isModified() ) {
    ce->accept();
    return;
    }

    switch( QMessageBox::information( this, "This is QAoolication Window Example",
                      "Do you want to save the changes"
                      " to the document?",
                      "Yes", "No", "Cancel",
                      0, 1 ) ) {
        case 0:
            save();
            ce->accept();
            break;
        case 1:
            ce->accept();
            break;
        case 2:
            default: 
        ce->ignore();
        break;
    }
}



/*关于QT*/
void window::aboutQt()
{
    QMessageBox::aboutQt( this, "This is QAoolication Window Example" );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -