Results 1 to 3 of 3

Thread: Open and display a document on a window

  1. #1
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Open and display a document on a window

    Hello!
    I want to open a text document and to display it on my window. The process has two actions: Opening the file and displaying it on the window. Theoritically, two slots are expected, and two bottons too to be clicked on. But practically, it seems not feasible, since a click on the open button should display the data on the window.
    If I click on m_open (see below), a dialog window for the selection of the file will open. I have to selcet this file, click on "open" button of the Dialog window and then the window dissapears.

    Line 8 of .cpp: The text erea shoul dremain hide until the data a ready to be display, and will appear with the line 25 of .cpp.
    On the line 27 of .cpp file, I display the data on the window. If is not here, I must create another slot, that requires another button, because when I am connecting the button "m_open" to this slot (to open the file) and to another(to display the data), the compilation fails.

    My questions:
    1- Is there a way to connect the button open of the dialog window (when opening a file) to a slot in order to display my data on my window? If not, how to proceed?

    Many thanks in advance.

    Here is my code:

    .h flie
    Qt Code:
    1. #ifndef DEF_MYWINDOW
    2. #define DEF_MYWINDOW
    3.  
    4. #include <QtGui>
    5.  
    6.  
    7. class MyWindow: public QMainWindow
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. explicit MyWindow();
    13. virtual ~MyWindow();
    14.  
    15. public slots:
    16. void openFile();
    17.  
    18. private:
    19. QAction *m_open;
    20. QTextEdit *m_myText;
    21. QWidget *m_centralZone;
    22. QMenu *m_menuFile;
    23. QLineEdit *m_openAFile;
    24.  
    25. };
    26.  
    27. #endif // MYWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    .cpp file
    Qt Code:
    1. #include "myWindow.h"
    2.  
    3. myWindow::myWindow()
    4. {
    5. m_menuFile = menuBar()->addMenu("&File");
    6.  
    7. m_myText = new QTextEdit(m_centralZone);
    8. m_myText->setVisible(false);
    9.  
    10. m_open = m_menuFile->addAction("Open a file");
    11.  
    12. m_openAFile = new QLineEdit;
    13.  
    14. connect(m_open, SIGNAL(triggered()), this, SLOT(openFile()));
    15.  
    16.  
    17. setCentralWidget(centralZone);
    18. }
    19.  
    20. void myWindow::openFile()
    21. {
    22. QString file = QFileDialog::getOpenFileName(this, "Information", "Open", "*.text");
    23. m_openAFile->setPlainText(file);
    24.  
    25. myText->setVisible(true);
    26.  
    27. myText->setText(text);
    28.  
    29. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Open and display a document on a window

    I guess this is what you wanted
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class MyWindow: public QMainWindow
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. explicit MyWindow();
    9. virtual ~MyWindow() {};
    10.  
    11. signals:
    12. void fileSelected(const QString & filename);
    13.  
    14. public slots:
    15. void openFile();
    16. void loadFile(const QString & filename);
    17.  
    18. private:
    19. QAction *m_open;
    20. QTextEdit *m_myText;
    21. QWidget *m_centralZone;
    22. QMenu *m_menuFile;
    23. QLineEdit *m_openAFile;
    24. };
    25.  
    26. MyWindow::MyWindow() : QMainWindow()
    27. {
    28. m_menuFile = menuBar()->addMenu("&File");
    29.  
    30. m_myText = new QTextEdit(m_centralZone);
    31. m_myText->setVisible(false);
    32.  
    33. m_open = m_menuFile->addAction("Open a file");
    34.  
    35. m_openAFile = new QLineEdit;
    36.  
    37. connect(m_open, SIGNAL(triggered()), this, SLOT(openFile()));
    38. connect(this, SIGNAL(fileSelected(QString)), this, SLOT(loadFile(QString)));
    39.  
    40. setCentralWidget(m_centralZone);
    41. }
    42.  
    43. void MyWindow::openFile()
    44. {
    45. QString file = QFileDialog::getOpenFileName(this, "Information", "Open", "*.text");
    46. m_openAFile->setText(file);
    47. emit fileSelected(file);
    48. }
    49.  
    50. void MyWindow::loadFile(const QString & filename)
    51. {
    52. QFile file(filename);
    53. if(file.open(file.Text | file.ReadOnly))
    54. {
    55. QString text = file.readAll();
    56. m_myText->setText(text);
    57. }
    58. else
    59. m_myText->setText("Error Opening File");
    60.  
    61. m_myText->setVisible(true);
    62. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Open and display a document on a window

    Incredible!!!!!!
    Reddy, you a really an engel!!!

    Many thanks!! It works PERFECTLY!!!

    Many thanks one more time Reddy!

Similar Threads

  1. can we display a windows document (.doc)?????
    By criticalboot in forum Newbie
    Replies: 3
    Last Post: 17th June 2009, 08:31
  2. OpenOffice document display on QTextBrowser solution
    By patrik08 in forum Qt-based Software
    Replies: 8
    Last Post: 17th September 2008, 00:55
  3. open a document
    By peace_comp in forum Qt Programming
    Replies: 5
    Last Post: 30th June 2008, 07:08
  4. can Qt open MS word document?
    By coder1985 in forum Qt Programming
    Replies: 1
    Last Post: 25th December 2007, 14:34
  5. How to open a document with the default application?
    By SkripT in forum Qt Programming
    Replies: 7
    Last Post: 30th April 2006, 08:47

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.