#ifndef ADDRESSBAR_H#define ADDRESSBAR_H#include <QWidget>#include <QLineEdit>#include <QPushButton>#include <QHBoxLayout>#include <QUrl>#include <QString>class AddressBar : public QWidget{ Q_OBJECTpublic: explicit AddressBar(QWidget *parent = 0); signals: void go(QUrl); public slots: void goToSite();private: QLineEdit *addressEdit; QPushButton *goButton; QHBoxLayout *layout;};#endif // ADDRESSBAR_H
#include "addressbar.h"AddressBar::AddressBar(QWidget *parent) : QWidget(parent){ addressEdit = new QLineEdit(parent); goButton = new QPushButton("Go", parent); layout = new QHBoxLayout; layout->addWidget(addressEdit); layout->addWidget(goButton); this->setLayout(layout); connect(goButton, SIGNAL(clicked()), this, SLOT(goToSite())); connect(addressEdit, SIGNAL(returnPressed()), this, SLOT(goToSite()));}void AddressBar::goToSite(){ QString address = addressEdit->text(); emit go(QUrl(address));}
#ifndef HTMLVIEW_H#define HTMLVIEW_H#include <QWebView>class HtmlView : public QWebView{ Q_OBJECTpublic: explicit HtmlView(QWidget *parent = 0); signals: public slots: void loadNewPage(const QUrl &url); };#endif // HTMLVIEW_H
#include "htmlview.h"HtmlView::HtmlView(QWidget *parent) : QWebView(parent){}void HtmlView::loadNewPage(const QUrl &url){ this->load(url);}
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QWidget>#include <QLineEdit>#include <QPushButton>#include <QGridLayout>#include <QtWebKit>#include <QMainWindow>#include "addressbar.h"#include "htmlview.h"class MainWindow : public QMainWindow{ Q_OBJECT public: explicit MainWindow(QWidget *parent = 0);};#endif // MAINWINDOW_H
#include "mainwindow.h"#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){ // 1.Create widget QWidget *centralWidget = new QWidget(this); AddressBar *bar = new AddressBar; HtmlView *view = new HtmlView; // 2.Add widget to layout QGridLayout *layout = new QGridLayout; layout->addWidget(bar, 0, 0, 1, 10); layout->addWidget(view, 1, 0, 1, 10); centralWidget->setLayout(layout); // 3.Connect widget QObject::connect(bar, SIGNAL(go(QUrl)), view, SLOT(loadNewPage(QUrl))); this->setCentralWidget(centralWidget); this->setWindowTitle("My Browser v1.0"); this->resize(640, 480);}
#include <QApplication>#include "mainwindow.h"int main(int argc, char *argv[]){ QApplication app(argc, argv); MainWindow *window = new MainWindow; window->show(); return app.exec();}
QPushButton::event -> QAbstractButton::event ->QWidget::event [Dispatch event]: case QEvent::KeyPress: { QKeyEvent *k = (QKeyEvent *)event; bool res = false; if (!(k->modifiers() & (Qt::ControlModifier | Qt::AltModifier))) { //### Add MetaModifier? if (k->key() == Qt::Key_Backtab || (k->key() == Qt::Key_Tab && (k->modifiers() & Qt::ShiftModifier))) res = focusNextPrevChild(false); else if (k->key() == Qt::Key_Tab) res = focusNextPrevChild(true); if (res) break; } keyPressEvent(k);-> QPushButton::keyPressEventvoid QPushButton::keyPressEvent(QKeyEvent *e) { Q_D(QPushButton); switch (e->key()) { case Qt::Key_Enter: case Qt::Key_Return: if (autoDefault() || d->defaultButton) { click(); break; } // fall through default: QAbstractButton::keyPressEvent(e); } }-> QAbstractButton::click:void QAbstractButton::click() { if (!isEnabled()) return; Q_D(QAbstractButton); QPointer<QAbstractButton> guard(this); d->down = true; d->emitPressed(); if (guard) { d->down = false; nextCheckState(); if (guard) d->emitReleased(); if (guard) d->emitClicked(); } }
联系客服