LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-06-2021, 11:19 AM   #1
teckk
Senior Member
 
Registered: Oct 2004
Distribution: FreeBSD Arch
Posts: 3,619
Blog Entries: 2

Rep: Reputation: 1163Reputation: 1163Reputation: 1163Reputation: 1163Reputation: 1163Reputation: 1163Reputation: 1163Reputation: 1163Reputation: 1163
C++ Practice with webkit.


Everything is hard coded which is not ideal, but makes the code small, and shows how everything works. I decided to study C and C++ at the same time.

You'll need g++ and qt5 webkit installed.

wb.cpp
Code:
//Basic Qt5Webkit Web browser in c++

#include <QApplication>
#include <QWebView>
#include <QtWidgets>
#include <iostream>

//Browser Home Page
const char* HomePage = "https://www.linuxquestions.org/questions/";

//User Agent iphone10 safari
const char* AgentIphone = "Mozilla/5.0 (iPhone; CPU iPhone OS 10_0_1 "
    "like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) "
    "Version/10.0 Mobile/14A403 Safari/602.1";
    
//User Agent Win10 firefox
const char* AgentWin10 = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; "
    "rv:90.0) Gecko/20100101 Firefox/90.0'";
    
//Change User Agent class
class UserAgent : public QWebPage 
{
    QString userAgentForUrl(const QUrl &Url) const 
    {
        QString UA = AgentIphone; //User Agent here
        return QString(UA);
    }
};

int main(int argc, char** argv) 
{
    QApplication app(argc, argv);
    app.setApplicationName(QString("My Browser")); //Browser name
    app.setApplicationVersion(QString("Version .1a")); //Version
    
    //Web Browser settings
    QWebView View;
    View.setPage(new UserAgent());
    View.setZoomFactor(1.5);
    View.settings()->setAttribute(QWebSettings::JavascriptEnabled, false);
    View.settings()->setAttribute(QWebSettings::AutoLoadImages, false);
    View.settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
    View.settings()->setAttribute(QWebSettings::PluginsEnabled, false);
    View.settings()->setAttribute(QWebSettings::LocalStorageEnabled, false);
    View.settings()->setFontFamily(QWebSettings::StandardFont, "monospace");
    View.settings()->globalSettings()->setFontSize(QWebSettings::MinimumFontSize, 18);

    //Positional Arguments
    QCommandLineParser cLineParser;
    cLineParser.addPositionalArgument(QStringLiteral("Url"), QStringLiteral()); 
    cLineParser.process(app);
    QStringList posArgs = cLineParser.positionalArguments();
    QUrl Url;
    
    //Get args or open HomePage
    if (posArgs.size() > 1)
    {
        std::cout << "Too many arguments!" << std::endl;
        return 0;
    } else if (posArgs.size() == 1)
    {
        Url = QUrl::fromUserInput(posArgs.at(0));
    } else
    {
        Url = QUrl(HomePage);
    }
    
    //Set Url for WebView
    View.setUrl(Url);
    
    //Size window
    View.resize(1400, 1000);
    View.show();
    return app.exec();
}
wb.pro
Code:
QT += webkit
QT += webkitwidgets
QT += network 
QT += widgets

SOURCES = wb.cpp
qmake should make .qmake.stash and Makefile in the directory. Then compile with make

Last edited by teckk; 09-06-2021 at 11:36 AM.
 
Old 09-06-2021, 11:30 AM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 1,618

Rep: Reputation: 1315Reputation: 1315Reputation: 1315Reputation: 1315Reputation: 1315Reputation: 1315Reputation: 1315Reputation: 1315Reputation: 1315Reputation: 1315

Doesn't the compiler complain about "flase" on line 41?

 
Old 09-06-2021, 11:36 AM   #3
teckk
Senior Member
 
Registered: Oct 2004
Distribution: FreeBSD Arch
Posts: 3,619

Original Poster
Blog Entries: 2

Rep: Reputation: 1163Reputation: 1163Reputation: 1163Reputation: 1163Reputation: 1163Reputation: 1163Reputation: 1163Reputation: 1163Reputation: 1163
Oh thank you, it sure would. Fixed.
 
Old 09-06-2021, 11:37 AM   #4
teckk
Senior Member
 
Registered: Oct 2004
Distribution: FreeBSD Arch
Posts: 3,619

Original Poster
Blog Entries: 2

Rep: Reputation: 1163Reputation: 1163Reputation: 1163Reputation: 1163Reputation: 1163Reputation: 1163Reputation: 1163Reputation: 1163Reputation: 1163
More C++ Practice. No tool kits needed, just C++.

tempconv.cpp
Code:
//Temperature converter

#include <iostream>

//Term Colors
#define RESET "\033[0m"
#define BOLD  "\033[1m"
#define CLR   "\033[2J\033[1;1H";
#define FGBLK "\033[30m"
#define FGRED "\033[31m"
#define FGGRN "\033[32m" 
#define FGYEL "\033[33m"
#define FGBLU "\033[34m"
#define FGMAG "\033[35m"
#define FGCYA "\033[36m"
#define FGWHI "\033[37m"

#define BGBLK "\033[40m"
#define BGRED "\033[41m"
#define BGGRN "\033[42m"
#define BGYEL "\033[43m"
#define BGBLU "\033[44m"
#define BGMAG "\033[45m"
#define BGCYA "\033[46m"
#define BGWHI "\033[47m"

int main()
{
    std::cout << CLR;
    int choice;
    do
    {
        std::cout << BGRED << FGWHI << "----" << "Temperature Converter----" 
        << RESET << std::endl;
        
        std::cout   << std::endl
                    << "0. Quit" << "\n" 
                    << "1. Celsius to Fahrenheit" << "\n"
                    << "2. Fahrenheit to Celsius" << "\n"
                    << "3. Celcius to Kelvin" << "\n"
                    << "4. Kelvin to Celcius" << "\n"
                    << "5. Fahrenheit to Kelvin" << "\n"
                    << "6. Kelvin to Fahrenheit" << "\n"
                    << std::endl;
                    
        std::cout << "Select an option: ";
        std::cin >> choice;

        switch(choice)
        {
            case 0:
                std::cout << "Exiting..." << std::endl;
                return 0;
                
            case 1:
            {
                double CeltoFahr;
                std::cout << "Enter Deg Celsius: ";
                std::cin >> CeltoFahr;
                double Fahrenheit = (CeltoFahr * 9/5) + 32;
                std::cout << CLR;
                std::cout << BGYEL << FGBLK << CeltoFahr 
                    << "C = " << Fahrenheit << "F" << RESET << "\n\n";
                break;
            }

            case 2:
            {
                double FahrtoCel;
                std::cout << "Enter Deg Fahrenheit: ";
                std::cin >> FahrtoCel;
                double Celsius = (FahrtoCel - 32)* 5/9;
                std::cout << CLR;
                std::cout << BGYEL << FGBLK << FahrtoCel 
                    << "F = " << Celsius << "C" << RESET << "\n\n";
                break;
            }
            
            case 3:
            {
                double CeltoKelv;
                std::cout << "Enter Deg Celsius: ";
                std::cin >> CeltoKelv;
                double ctk =  CeltoKelv + 273.15;
                std::cout << CLR;
                std::cout << BGYEL << FGBLK << CeltoKelv 
                    << "C = " << ctk << "K" << RESET << "\n\n";
                break;
            }
            
            case 4:
            {
                double KelvtoCel;
                std::cout << "Enter Deg Kelvin: ";
                std::cin >> KelvtoCel;
                double ktc = KelvtoCel - 273.15;
                std::cout << CLR;
                std::cout << BGYEL << FGBLK << KelvtoCel 
                    << "K = " << ktc << "C" << RESET << "\n\n";
                break;
            }
            
            case 5:
            {
                double FahrtoKelv;
                std::cout << "Enter Deg Fahrenheit: ";
                std::cin >> FahrtoKelv;
                double ftk =  (FahrtoKelv + 459.67)* 5/9;
                std::cout << CLR;
                std::cout << BGYEL << FGBLK << FahrtoKelv 
                    << "F = " << ftk << "K" << RESET << "\n\n";
                break;
            }
            
            case 6:
            {
                double KelvtoFahr;
                std::cout << "Enter Deg Kelvin: ";
                std::cin >> KelvtoFahr;
                double ktf = (KelvtoFahr * 9/5) - 459.67;
                std::cout << CLR;
                std::cout << BGYEL << FGBLK << KelvtoFahr 
                    << "K = " << ktf << "F" << RESET << "\n\n";
                break;
            }
                
            default:
                std::cout << CLR;
                std::cout << BGYEL << FGBLK << choice 
                    << " is not an option" << std::endl;
                break;
        }
    }
    while (choice != 0);

}
Compile with:
Code:
g++ tempconv.cpp -o tempconv
 
Old 09-06-2021, 02:11 PM   #5
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,715
Blog Entries: 11

Rep: Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747Reputation: 3747
Thank you for sharing your projects, it is always great to see someone enthusiastic about learning new things!

However, simply posting code, even complete, cool code is not really a good use of the Programming forum as it is not intended to be a repo but a place where others can find help for specific questions and discuss those with others. Code may produce a lot of search results but those are rarely relevant to others searching for help with specific problems.

I would not want to discourage you from sharing your learning experience but would suggest that you post examples of specific programming idioms or examples along with a more complete description of the idea behind them. Pose a problem to be solved, then solve it in a simple way that can be easily understood by others. Doing so will generate discussion, suggestions from others and a sharing of knowledge instead of code, which is what the Programming forum in particular, is all about as explained in the Site FAQ!

If you just want to share your completed code without much other discussion I would suggest that you make use of your blog space here at LQ. There you can post your complete project code, maintain it over time with updates and even differing versions, then reference those blog pages as examples of use in more specific posts to the forum.

Good luck!
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: KHTML Vs Webkit: To Merge or Not To Merge LXer Syndicated Linux News 0 10-27-2007 06:41 AM
Questions about WebKit, KHTML, C, GTK2 Jorophose Programming 0 09-30-2007 02:12 PM
LXer: Quickies: KDE e.V. Presidential Address, KHTML and WebKit, Qt4 Book, KDE4 on Mac Visuals LXer Syndicated Linux News 0 07-24-2007 04:16 AM
LXer: Video: Lars Knoll and George Staikos on KHTML and WebKit LXer Syndicated Linux News 0 12-13-2006 07:21 PM
LXer: WebKit Ported to Qt 4 LXer Syndicated Linux News 0 07-12-2006 06:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration