// My cpp.cpp
#include "h.h"
#include <QApplication>
#include <QFileInfo>
#include <QMessageBox>
#include <QLabel>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QFileDialog>
ExternalApplicationWidget
::ExternalApplicationWidget(QWidget *parent
) : QWidget(parent
){
selectPDFWidget->setObjectName("selectPDFWidget");
filepathLabel
= new QLabel(this);
filepathLabel->setObjectName("filepathLabel");
hbox->addWidget(filepathLabel);
button->setObjectName("button");
hbox->addWidget(button);
connect(button, SIGNAL(clicked()), this, SLOT(openPDF()));
pdfWidget->setAutoFillBackground( false );
pdfWidget->setAttribute(Qt::WA_NoSystemBackground, true);
pdfWidget->setAttribute(Qt::WA_OpaquePaintEvent, true);
pdfWidget->setObjectName("pdfWidget");
vbox->addWidget(selectPDFWidget, 1);
vbox->addWidget(pdfWidget, 5);
}
void ExternalApplicationWidget
::loadUrl( const QString &fileName
) {
return;
}
QString pth
= qApp
->applicationDirPath
() + "/SumatraPDF.exe";
if( !QFile::exists(pth
) ) { QMessageBox::critical(this,
"Red alert!",tr
("SumatraPDF.exe could not be loaded!"),tr
("&Ok"));
return;
}
filepathLabel->setText(pdf_filepath);
args << "-zoom" << "fit page";
args << pdf_filepath;
args <<
"-plugin" <<
QString::number((qulonglong
)pdfWidget
->winId
());
startProcess("SumatraPDF.exe",args);
}
void ExternalApplicationWidget
::startProcess(const QString &program,
const QStringList &arguments
) {
p->start(program, arguments); // steals "focus" !!!
p->waitForStarted();
}
void ExternalApplicationWidget::openPDF()
{
QString fileName
= QFileDialog::getOpenFileName(this, tr
("Open PDF"),
"C:/temp", tr
("PDF Files (*.pdf)"));
if( fileName.isEmpty() ) {
return;
}
loadUrl(fileName);
}
#include <QHBoxLayout>
#include <QMenuBar>
#include <QStatusBar>
#include <QApplication>
MainWindow
::MainWindow(QWidget *parent
) ,labelKeypressCount(0)
{
setupUi();
}
MainWindow::~MainWindow()
{
}
void MainWindow::setupUi()
{
int width = 1400;
int height = 1500;
centralwidget
->setObjectName
(QString::fromUtf8("centralwidget"));
horizontalLayoutWidget
->setObjectName
(QString::fromUtf8("horizontalLayoutWidget"));
horizontalLayoutWidget
->setGeometry
(QRect(59,
29, width, height
));
horizontalLayout
->setObjectName
(QString::fromUtf8("horizontalLayout"));
horizontalLayout->setContentsMargins(0, 0, 0, 0);
MyCustomLabel* labelWidget = new MyCustomLabel(horizontalLayoutWidget);
labelWidget
->setObjectName
(QString::fromUtf8("MyCustomLabel"));
labelWidget->setText("The status bar counts key presses on this label!");
horizontalLayout->addWidget(labelWidget);
ExternalApplicationWidget* externalApplicationWidget= new ExternalApplicationWidget(horizontalLayoutWidget);
externalApplicationWidget
->setObjectName
(QString::fromUtf8("ExternalApplicationWidget"));
horizontalLayout->addWidget(externalApplicationWidget);
setCentralWidget(centralwidget);
statusbar
->setObjectName
(QString::fromUtf8("statusbar"));
setStatusBar(statusbar);
connect(labelWidget, SIGNAL(keyPressed(int)), this, SLOT(keyPressOnLabel(int )));
connect(this, SIGNAL(updateStatusBar(const QString& ,int)), statusbar, SLOT(showMessage(const QString&, int )));
if( qApp ) {
}
resize(width+100, height+100);
}
QString MainWindow
::updateMessage() {
QString msg
= "Current focus widget is ";
msg += "'";
msg += name ;
msg += "'";
} else {
msg += "NONE";
}
msg += " --- ";
msg += "\n";
msg += "Number of key presses = ";
msg
+= QString::number(labelKeypressCount
);
return msg;
}
void MainWindow
::applicationFocusChanged(QWidget* old,
QWidget* current
) {
emit updateStatusBar(updateMessage(), 0);
}
void MainWindow::keyPressOnLabel(int updateCount)
{
labelKeypressCount=updateCount;
emit updateStatusBar(updateMessage(), 0);
}
#include <QDebug>
MyCustomLabel
::MyCustomLabel(QWidget *parent
) : QLabel(parent
), countKeypresses
(0){
setWordWrap(true);
}
{
qDebug() << "label clicked";
setFocus();
}
void MyCustomLabel
::keyPressEvent(QKeyEvent *ev
) {
qDebug() << "label: key pressed";
countKeypresses++;
emit keyPressed(countKeypresses);
}