Very Small docx Content View with QAxWidget



  • #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QAxObject>
    #include <QAxWidget>
    #include <QString>
    #include <QTextEdit>
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = 0);
        ~MainWindow();
        void open(const QString&);
    private:
        QAxWidget* axw;
    };
    
    #endif // MAINWINDOW_H
    
    
    #include "mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
    }
    
    MainWindow::~MainWindow()
    {
    
    }
    
    void MainWindow::open(const QString &fnm)
    {
        if(fnm.isEmpty()) return;
        if(fnm.endsWith("doc") || fnm.endsWith("docx")){
            axw = new QAxWidget("Word.Application", nullptr);
            setCentralWidget(axw);
            axw->setGeometry(0,0,1024,700);
            axw->setControl(fnm);
            axw->show();
        }
    }
    
    
    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.setGeometry(20,20,1366,768);
        w.show();
    
        w.open("C:/Users/xxx/Desktop/test.docx");
    
        return a.exec();
    }
    
    

    /////////
    When I test with doc file, every thing is OK.


  • Moderators

    @Manifolds Did you try to call axw->setGeometry(0,0,1024,700); after axw->setControl(fnm); ?
    Does the docx contain anything? What is the page format in that docx?



  • @jsulm

    Just one sentence.

    My Qt Version is 5.8 win32 msvc visual studio 2015.
    My OS is Win10 64bit.
    My Office version is 2016 64bit


  • Moderators

    Hi,

    If you know the size you would prefer you could use :
    axw->setMinimumSize(1024,700);

    Eddy



  • @Eddy

    No , this can't change anything.

    http://pan.baidu.com/s/1o83AO7g


  • Moderators

    Hi Manifolds,

    The link you posted is dead, can you give us the correct link please?

    Eddy



  • @Eddy

    you can't view this file outside China.

    new link on OnDrive:

    https://1drv.ms/i/s!AukiP6uHSpGK1DlJ7uJYURjuqMmR


  • Moderators

    Hi Manifolds,

    instead of

            axw = new QAxWidget("Word.Application", nullptr);
            setCentralWidget(axw);
            axw->setGeometry(0,0,1024,700);
            axw->setControl(fnm);
            axw->show();
    

    Try this

            QAxWidget* axw=new QAxWidget ();
            axw->setControl(fnm);
            axw-> show ();
            setCentralWidget(axw);
    

    And the word document is filling up all available space of the QMainWindow as you wish. At least that is what I get.

    You are using "Word.application" instead of a "Word.Document". Those are different COM objects with different CLSID's. Using getDocumentation, you can check this out.
    I think the sizehints were messed up because of the order you call the functions.(you can check that with sizeHint())

    Normally I use a Word.application to start with, then one can get "Documents" from it using QuerySubObject and from Documents you get at Document, where you wanted to land directly.

    Out of curiosity, what is it you want to achieve exactly? Just opening one file or do you want to make an application that can open different documents?

    Cheers,

    Eddy



  • @Eddy

    Solved, Thank you very much.

    I just want to view the word file, a simple job.


  • Moderators

    You're welcome

    Eddy



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.