Giter Club home page Giter Club logo

Comments (8)

Archie3d avatar Archie3d commented on September 23, 2024

It should close the document. But because of conflict with QWidget::close() method, I've just renamed it to QPdfWidget::closeDocument(). It closes the document and resets the view to a default empty one.

from qpdf.

rahmanshaber avatar rahmanshaber commented on September 23, 2024

still not closing.it crashes the whole app.
can include the methode in your viewer??

here is my code

`
corepdf::corepdf(QWidget *parent):QWidget(parent)
{
// openPdfFile("/home/shaber/Desktop/p.pdf");

    setObjectName("corepdf");
    setStyleSheet("QWidget{background-color: #3E3E3E;border: 1px #2A2A2A;}");
 }

 corepdf::~corepdf()
 {
    delete PdfWidget;
 }

 void corepdf::eclose()
 {
    QPdfWidget *cpdf = this->findChild<QPdfWidget*>("QPdfWidget");
    cpdf->closeDocument();
 }

 void corepdf::openPdfFile(const QString path)
 {
    workFilePath = path;
    QVBoxLayout * mainLayout = new QVBoxLayout();
    PdfWidget = new QPdfWidget();

    connect(PdfWidget, &QPdfWidget::initialized, [this,path]() {
        PdfWidget->setToolbarVisible(false);
        QFile f(path);
        if (f.open(QIODevice::ReadOnly)) {
            QByteArray data = f.readAll();
            PdfWidget->loadData(data);
            f.close();
        }
    });

    cShot     = new QPushButton("close");
    connect(cShot,SIGNAL(clicked()),this,SLOT(eclose()));
    mainLayout->setContentsMargins(0,0,0,0);
    mainLayout->addWidget(PdfWidget);
    mainLayout->addWidget(cShot);
    setLayout(mainLayout);
 }

`

`
//header
class corepdf : public QWidget
{
Q_OBJECT

 public:
    corepdf(QWidget *parent = 0);
    ~corepdf();
    void openPdfFile(const QString path);


    QString workFilePath;

 protected:
    void closeEvent(QCloseEvent *event);
 public slots:
    void eclose();
 private:

    QPushButton * cShot;
    QPdfWidget *PdfWidget;
 };

`

from qpdf.

Archie3d avatar Archie3d commented on September 23, 2024

Are you sure the following code of yours

QPdfWidget *cpdf = this->findChild<QPdfWidget*>("QPdfWidget");

does not return null? Because you never assign "QPdfWidget" name to your widget.

Try adding the following when creating the widget:

QVBoxLayout * mainLayout = new QVBoxLayout();
PdfWidget = new QPdfWidget(); // BTW, do you really want to create a new widget every time you open a file?
PdfWidget->setObjectName("QPdfWidget"); // Without the name assignment findWidget() won't find anything

Or better create your PdfWidget in constructor and access it via member variable of your corepdf class.

from qpdf.

rahmanshaber avatar rahmanshaber commented on September 23, 2024

i can't add qpdfwidget in constructor .
there is some ui problem.

i added the object name .but still closing qpdfwidget is crashing the whole app.
i don't know how to check ,if the widget is found or not.

from qpdf.

Archie3d avatar Archie3d commented on September 23, 2024

Well, you check it against nullptr:

 void corepdf::eclose()
 {
    QPdfWidget *cpdf = this->findChild<QPdfWidget*>("QPdfWidget");
    if (cpdf != nullptr) {
        cpdf->closeDocument();
    } else {
        qWarning() << "Not found!";
    }
 }

from qpdf.

rahmanshaber avatar rahmanshaber commented on September 23, 2024

yes . widget is not found.

ge

from qpdf.

rahmanshaber avatar rahmanshaber commented on September 23, 2024

i found the solution .
QPdfWidget *cpdf = qobject_cast<QPdfWidget*>(this->children().at(1));

from qpdf.

Archie3d avatar Archie3d commented on September 23, 2024

This is not a good solution, which will break when you start changing the layout or add more children QObjects to your corepdf. I would recommend to capture cpdf pointer as a class member field instead.

from qpdf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.