[QT programming] caller

Source: Internet
Author: User

Tag: QT speaker system call

We know that the win7 system has its own narrator, that is, the machine can read the current content, the computer can be locked, and then click the button in the lower left corner. Previously, when I used MATLAB to write a mine clearance game, I also called the narrator to make voice prompts for the game.The specific MATLAB script file is as follows:

SP = actxserver ('sapi. spvoice '); sp. Speak ('hello, welcome to Xi'an University of electronic science and technology! Hello, welcome to XD University! ')

Qt calls the narrator, need to use a special class, specific can refer to the http://lynxline.com/qtspeech-say-hello-world article, this paper roughly introduces the use of this class. Next I will use this class to implement the speaker's call. First, create a dialog GUI project and add the header file speech. H of the qtspeech class mentioned above and the source file speech. cpp to the project, In this way, the project has five files.: Dialog. H, speech. H, Main. cpp, dialog. cpp, and speech. cpp. Of course, there is also the interface file dialog. UI. In the interface file Add a qtextedit ControlInput the text you want to read, and then add the pronunciation function of qtspeech to its slot function, Add the qpushbutton ControlTo control pronunciation. The source code of each file is as follows: 1. Dialog. h
#ifndef DIALOG_H#define DIALOG_H#include <QDialog>#include"speech.h"namespace Ui {class Dialog;}class Dialog : public QDialog{    Q_OBJECT    public:    explicit Dialog(QWidget *parent = 0);    ~Dialog();    private slots:    void on_pushButton_clicked();private:    Ui::Dialog *ui;};#endif // DIALOG_H
2. Speech. h

# Ifndef speech_h # define speech_h # include <qobject> class qtspeech: Public qobject {q_objectpublic: // handle error struct error {qstring MSG; error (qstring S): MSG (s) {}}; struct initerror: error {initerror (qstring S): Error (s) {}}; struct logicerror: error {logicerror (qstring S): Error (s) {}}; struct closeerror: error {closeerror (qstring S): Error (s) {}}; // define the data type struct voicename {qstring ID; Qstring name ;}; typedef qlist <voicename> voicenames; // define the constructor qtspeech (qobject * parent); qtspeech (voicename n = voicename (), qobject * parent = 0l ); virtual ~ Qtspeech (); const voicename & name () const; // static voicenames voices (); // the content to be read void say (qstring) const; // synchronous pronunciation void tell (qstring) const; // asynchronous pronunciation void tell (qstring, qobject * OBJ, const char * slot) const; // when the pronunciation ends, pause/********************/void pause (void) const; // pause void resume (void) const; // resume void stop (void) const from pause; // stop pronunciation/*******************/signals: void finished (); protected: Virtual void timerevent (qtimerevent *); Private: class private; private * D ;}; //} # endif/speech_h

3. Main. cpp

#include <QApplication>#include"dialog.h"int main(int argc, char *argv[]){    QApplication app(argc, argv);    Dialog dlg;    dlg.show();    return app.exec();}

4. Dialog. cpp

#include "dialog.h"#include "ui_dialog.h"Dialog::Dialog(QWidget *parent) :    QDialog(parent),    ui(new Ui::Dialog){    ui->setupUi(this);}Dialog::~Dialog(){    delete ui;}void Dialog::on_pushButton_clicked(){    QtSpeech *speaker = new QtSpeech(this);    speaker->tell(ui->textEdit->toPlainText(),speaker,SLOT(onSpeechFinished()));   // speaker.stop();}

5. Speech. cpp

# Include "speech. H "# include <qstring> # include <qpointer> # include <qlist> # include <qtimerevent> # UNDEF Unicode # include <SAPI. h> # include <sphelper. h> # include <comdef. h> # define Unicode # include <windows. h> # include <windowsx. h> # include <commctrl. h> // some defines for throwing exceptions # define where qstring ("% 1: % 2 :"). arg (_ file __). arg (_ line _) # define syscall (x, e) {hresult hR = x; If (failed (HR) {qstr Ing MSG = # E; MSG + = ":" + qstring (_ file _); MSG + = ":" + qstring: Number (_ line __) + ":" + # x + ":"; MSG + = _ com_error (HR ). errormessage (); throw E (MSG) ;}// internal dataclass qtspeech: Private {public: Private (): onfinishslot (0l), waitingfinish (false) {} voicename name; static const qstring voiceid; typedef qpointer <qtspeech> PTR; static qlist <PTR> ptrs; ccomptr <ispvoice> voice; const char * onfinishslot; Qpointer <qobject> onfinishobj; bool waitingfinish; Class wchar_holder {public: wchar * w; wchar_holder (qstring S): w (0) {W = new wchar [S. length () + 1]; S. towchararray (w); W [S. length ()] = 0 ;}~ Wchar_holder () {Delete [] W ;};}; const qstring qtspeech: Private: voiceid = qstring ("win: % 1"); qlist <qtspeech :: PRIVATE: PTR> qtspeech: Private: ptrs = qlist <qtspeech: Private: PTR> (); // class definition qtspeech: qtspeech (qobject * parent): qobject (parent), D (new private) {coinitialize (null); syscall (D-> voice. cocreateinstance (clsid_spvoice), initerror); voicename N; wchar * w_id = 0l; wchar * w_name = 0l; CC Omptr <ispobjecttoken> voice; syscall (D-> voice-> getvoice (& voice), initerror); syscall (spgetdescription (voice, & w_name), initerror ); syscall (voice-> GETID (& w_id), initerror); N. name = qstring: fromwchararray (w_name); N. id = qstring: fromwchararray (w_id); voice. release (); If (N. id. isempty () Throw initerror (where + "No Default voice in system"); D-> name = N; D-> ptrs <this;} qtspeech: qtspeech (voicen Ame N, qobject * parent): qobject (parent), D (new private) {ulong COUNT = 0; ccomptr <ienumspobjecttokens> voices; coinitialize (null ); syscall (D-> voice. cocreateinstance (clsid_spvoice), initerror); If (N. id. isempty () {wchar * w_id = 0l; wchar * w_name = 0l; ccomptr <ispobjecttoken> voice; syscall (D-> voice-> getvoice (& voice), initerror ); syscall (spgetdescription (voice, & w_name), initerror); sysca LL (voice-> GETID (& w_id), initerror); N. name = qstring: fromwchararray (w_name); N. id = qstring: fromwchararray (w_id); voice. release ();} else {syscall (spenumtokens (spcat_voices, null, null, & voices), initerror); syscall (voices-> getcount (& COUNT), initerror ); for (INT I = 0; I <count; ++ I) {wchar * w_id = 0l; ccomptr <ispobjecttoken> voice; syscall (voices-> next (1, & voice, null), initerror); sysc All (voice-> GETID (& w_id), initerror); qstring id = qstring: fromwchararray (w_id); If (ID = n. ID) D-> voice-> setvoice (VOICE); voice. release () ;}} if (N. id. isempty () Throw initerror (where + "No Default voice in system"); D-> name = N; D-> ptrs <this;} qtspeech ::~ Qtspeech () {d-> ptrs. removeall (this); delete D;} const qtspeech: voicename & qtspeech: Name () const {return D-> name;} qtspeech: voicenames qtspeech: Voices () {voicenames vs; ulong COUNT = 0; ccomptr <ienumspobjecttokens> voices; coinitialize (null); syscall (spenumtokens (spcat_voices, null, null, & voices), logicerror ); syscall (voices-> getcount (& COUNT), logicerror); For (INT I = 0; I <count; ++ I) {wchar * w_id = 0l; wchar * w_name = 0l; ccomptr <ispobjecttoken> voice; syscall (voices-> next (1, & voice, null), logicerror); syscall (spgetdescription (voice, & w_name ), logicerror); syscall (voice-> GETID (& w_id), logicerror); qstring id = qstring: fromwchararray (w_id); qstring name = qstring: fromwchararray (w_name ); voicename n = {ID, name}; vs <n; voice. release ();} return vs;} void qtspeech: Tell (qstring text) const {tell (text, 0l, 0l);} void qtspeech: Tell (qstring text, qobject * OBJ, const char * slot) const {If (D-> waitingfinish) Throw logicerror (where + "already waiting to finish speech"); D-> onfinishobj = OBJ; d-> onfinishslot = slot; If (OBJ & slot) connect (const_cast <qtspeech *> (this), signal (finished (), OBJ, slot ); d-> waitingfinish = true; const_cast <qtspeech *> (this)-> starttimer (100); Private: wchar_holder w_text (text ); syscall (D-> voice-> speak (w_text.w, spf_async | spf_is_not_xml, 0), logicerror);} void qtspeech: Say (qstring text) const {PRIVATE :: wchar_holder w_text (text); syscall (D-> voice-> speak (w_text.w, blank, 0), logicerror);} void qtspeech: timerevent (qtimer* event Te) {qobject:: timerevent (TE); If (D-> waitingfinish) {spvoicestatus es; D-> voice-> getstatus (& ES, null); If (es. dwrunningstate = sprs_done) {d-> waitingfinish = false; killtimer (Te-> timerid (); finished ();}}} /***********************/void qtspeech: Pause (void) const {// pause syscall (D-> voice-> pause (), logicerror);} void qtspeech: Resume () const {// restore syscall (D-> voice-> resume (), logicerror);} void qtspeech: Stop () const {// stop syscall (D-> voice-> speak (null, spf_purgebeforespeak, 0), logicerror )} /***************************///}


The program result is as follows:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.