main.cpp——simplebrowser

本文介绍了一个使用Qt框架的应用程序如何从命令行参数中解析URL并加载到浏览器窗口中的方法。通过检查命令行参数,判断最后一个参数是否为有效URL,如果是,则在启动时直接打开该URL。
#include "browser.h"
#include "browserwindow.h"
#include <QApplication>

QString getCommandLineUrlArgument()
{
    const QStringList args = QCoreApplication::arguments();
    if (args.count() > 1) {
        const QString lastArg = args.last();
        const bool isValidUrl = QUrl::fromUserInput(lastArg).isValid();
        if (isValidUrl)
            return lastArg;
    }
    return QString();
}

int main(int argc, char **argv)
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QApplication app(argc, argv);
    app.setWindowIcon(QIcon(QLatin1String(":simplebrowser.svg")));

    BrowserWindow *window = new BrowserWindow();
    Browser::instance().addWindow(window);

    const QString url = getCommandLineUrlArgument();
    if (!url.isEmpty())
        window->loadPage(url);
    else
        window->loadHomePage();

    return app.exec();
}
SimpleBrowser是专门为自动化任务而设计的一个灵活而直观的浏览器引擎,内置.Net 4 framework。示例代码:class Program {     static void Main(string[] args)     {         var browser = new Browser();         try         {             // log the browser request/response data to files so we can interrogate them in case of an issue with our scraping             browser.RequestLogged  = OnBrowserRequestLogged;             browser.MessageLogged  = new Action<Browser, string>(OnBrowserMessageLogged);             // we'll fake the user agent for websites that alter their content for unrecognised browsers             browser.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10";             // browse to GitHub             browser.Navigate("http://github.com/");             if(LastRequestFailed(browser)) return; // always check the last request in case the page failed to load             // click the login link and click it             browser.Log("First we need to log in, so browse to the login page, fill in the login details and submit the form.");             var loginLink = browser.Find("a", FindBy.Text, "Login");             if(!loginLink.Exists)                 browser.Log("Can't find the login link! Perhaps the site is down for maintenance?");             else             {                 loginLink.Click();                 if(LastRequestFailed(browser)) return;                 // fill in the form and click the login button - the fields are easy to locate because they have ID attributes                 browser.Find("login_field").Value = "youremail@domain.com";                 browser.Find("password").Value = "yourpassword";                 browser.Find(ElementType.Button, "name", "commit").Click();                 if(LastRequestFailed(browser)) return;                 // see if the login succeeded - ContainsText() is very forgiving, so don't worry about whitespace, casing, html tags separating the text, etc.                 if(browser.ContainsText("Incorrect login or password"))                 {                     browser.Log("Login failed!", LogMessageType.Error);                 }                 else                 {                     // After logging in, we should check that the page contains elements that we recognise                     if(!browser.ContainsText("Your Repositories"))                         browser.Log("There wasn't the usual login failure message, but the text we normally expect isn't present on the page");                     else                     {                         browser.Log("Your News Feed:");                         // we can use simple jquery selectors, though advanced selectors are yet to be implemented                         foreach(var item in browser.Select("div.news .title"))                             browser.Log("* "   item.Value);                     }                 }             }         }         catch(Exception ex)         {             browser.Log(ex.Message, LogMessageType.Error);             browser.Log(ex.StackTrace, LogMessageType.StackTrace);         }         finally         {             var path = WriteFile("log-"   DateTime.UtcNow.Ticks   ".html", browser.RenderHtmlLogFile("SimpleBrowser Sample - Request Log"));             Process.Start(path);         }     }     static bool LastRequestFailed(Browser browser)     {         if(browser.LastWebException != null)         {             browser.Log("There was an error loading the page: "   browser.LastWebException.Message);             return true;         }         return false;     }     static void OnBrowserMessageLogged(Browser browser, string log)     {         Console.WriteLine(log);     }     static void OnBrowserRequestLogged(Browser req, HttpRequestLog log)     {         Console.WriteLine(" -> "   log.Method   " request to "   log.Url);         Console.WriteLine(" <- Response status code: "   log.ResponseCode);     }     static string WriteFile(string filename, string text)     {         var dir = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs"));         if(!dir.Exists) dir.Create();         var path = Path.Combine(dir.FullName, filename);         File.WriteAllText(path, text);         return path;     } }
在project1中,源.cpp界面执行后调试控制面板只有main.cpp输出结果,可能有以下几种原因: ### 代码逻辑问题 - 源.cpp中的代码可能没有被正确调用。在C++项目里,程序的入口通常是main.cpp中的`main`函数。若源.cpp里的函数或类没有在main.cpp中被调用,这些代码就不会被执行,自然不会有输出。例如,若源.cpp里定义了一个函数`void sourceFunction()`,但在main.cpp中没有调用`sourceFunction()`,那么源.cpp的代码就不会被执行。 ```cpp // 源.cpp #include <iostream> void sourceFunction() { std::cout << "This is from source.cpp" << std::endl; } // main.cpp #include <iostream> int main() { std::cout << "This is from main.cpp" << std::endl; // 若没有下面这行调用,源.cpp的代码不会执行 // sourceFunction(); return 0; } ``` ### 头文件包含问题 - 若源.cpp中定义的函数或类依赖特定的头文件,而这些头文件没有被正确包含,可能会导致编译错误或者代码无法正常执行。根据引用,源文件需要包含实现的头文件以及所需的头文件,且包含自己写的头文件时要用双引号"" [^1]。若头文件包含有误,源.cpp中的代码可能无法正常编译或者执行。 ```cpp // 源.cpp #include "myHeader.h" // 正确包含自己的头文件 #include <iostream> // 函数实现 ``` ### 编译和链接问题 - 编译过程中,若源.cpp没有被正确编译或者链接到项目中,那么其代码不会被执行。例如,在使用CMake构建项目时,若CMakeLists.txt文件没有正确配置,源.cpp可能不会被编译。在引用的示例中,添加自定义头文件和源文件后,需要修改CMakeLists.txt文件 [^2]。若配置有误,源.cpp可能不会被包含在项目的编译范围内。 ### 调试器配置问题 - 调试器可能没有正确配置以执行源.cpp中的代码。有时候,调试器可能只针对main.cpp进行调试,而忽略了其他源文件。需要检查调试器的配置,确保所有源文件都在调试范围内。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值