Filling window with a wxWebView - c++

I would like to fill an application window created with wxWidgets with a wxWebView. After reviewing the wxWebView sample, there the author created a wxFrame which then contained a wxBoxSizer and a wxWebView was added directly to that. However I would like my application to use a wxSingleChoiceDialog, so as far as I can tell, that means I need to include something like a wxPanel to use as the dialog's parent (the first argument to the constructor is the 'parent' of type wxWindow). So in my application I put a wxPanel in the wxFrame and then set the wxBoxSizer of the wxPanel to include the wxWebView. Unfortunately now the wxWebView no longer fits the full application window and I'm not sure why. Here is a simplified example of the application I'm trying to create.
main.h
#ifndef MAIN_H
#define MAIN_H
#include <wx/wx.h>
class MyApp : public wxApp
{
public:
virtual bool OnInit();
};
DECLARE_APP(MyApp)
#endif
main.cpp
#include "main.h"
#include "mainframe.h"
IMPLEMENT_APP(MyApp)
bool MyApp::OnInit()
{
MainFrame *mainFrame = new MainFrame(wxT("Content Creator"));
mainFrame->Show(true);
return true;
}
mainframe.h
#ifndef MAINFRAME_H
#define MAINFRAME_H
#include <wx/wx.h>
#include <wx/webview.h>
class MainFrame : public wxFrame
{
public:
wxPanel *panel;
wxBoxSizer *topsizer;
wxMenuBar *menubar;
wxMenu *file;
wxWebView *webView;
wxString editorURL;
MainFrame(const wxString& title);
void initMenu();
void initEditor();
};
#endif
mainframe.cpp
#include "mainframe.h"
MainFrame::MainFrame(const wxString& title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(800,600))
{
panel = new wxPanel(this, -1);
topsizer = new wxBoxSizer(wxVERTICAL);
panel->SetSizer(topsizer);
initMenu();
initEditor();
}
void MainFrame::initMenu()
{
menubar = new wxMenuBar;
file = new wxMenu;
//file->Append(wxID_EXIT, wxT("Quit"));
menubar->Append(file, wxT("File"));
SetMenuBar(menubar);
}
void MainFrame::initEditor()
{
editorURL = "file:///C:/Users/kenwood/Desktop/MarkCreator2/ckeditor/samples/editor.html";
webView = wxWebView::New(panel, wxID_ANY, editorURL);
topsizer->Add(webView, 1, wxEXPAND | wxALL, 0);
}
Here is a screenshot of the application this creates: Screenshot
Does anyone know how I might be able to make this wxWebView fill the application window (or at least why it isn't fitting the window because I'm clearly missing something here)? I realize I can create a wxWebView an specify a a size that would be equal to the window size, but then when the user resizes the screen webview is no longer the same size as the application window.
Thanks.
EDIT:
Also, I'm using wxWidgets version 2.9.4
EDIT 2:
It appears that calling initEditor() before initMenu() produces the desired result (but not the other way around). Would anyone know what the reason for this is? I would really like to call initMenu() first if that is possible.

You need to add the webView to the panels sizer. Your question actually doesn't have anything to do with wxWebView but just concerns the layout and I strongly recommend (re)reading the corresponding section of the manual.

For anyone having similar problems, one of the main problems with my code is that I was creating the 'panel' before creating the menubar. By calling initMenu() as the first function call in the constructor I was able to get the webview to display in the full application window --advice provided by the illustrious 'doublemax' of wxWidgets forums. Thanks for everyone's help.

Related

wxFilePickerCtrl not actualizing widget after setting path

I've got a problem with wxFilePickerCtrl. I tried to set a path with method SetPath(), and it did not work, the path is setted, because i can get it from GetPath(), but the widget still displays path (None).
Here is my code(simplified to two classes). Do you have any idea how can I does it properly? I tried diffrent wxFilePickerCtrl methods from documentation, but results were the same.
main.hpp:
#ifndef MAIN_HPP
#define MAIN_HPP
#include <wx/wx.h>
#include "panel.hpp"
class MyApp : public wxApp
{
public:
bool OnInit();
private:
Panel* _panel;
};
#endif
main.cpp:
#include "main.hpp"
wxIMPLEMENT_APP(MyApp)
bool MyApp::OnInit()
{
_panel = new Panel();
_panel->Show(true);
return true;
}
panel.hpp:
#ifndef PANEL_HPP
#define PANEL_HPP
#include <wx/wx.h>
#include <wx/filepicker.h>
class Panel : public wxFrame
{
public:
Panel();
private:
wxFilePickerCtrl* _filePicker;
wxBoxSizer* _sizer;
};
#endif
panel.cpp:
#include "panel.hpp"
Panel::Panel() : wxFrame(NULL, wxID_ANY, "MyApp", wxDefaultPosition, wxSize(850, 450))
{
_sizer = new wxBoxSizer(wxVERTICAL);
_filePicker = new wxFilePickerCtrl(this, wxID_ANY, "filePicker");
sizer->Add(_filePicker, 2, wxALIGN_CENTER, 2);
//file does not matter so in examplie I tried to set to main.cpp
_filePicker->SetPath("/some_path/main.cpp");
this->SetSizer(_sizer);
this->Centre();
}
This is a GTK limitation which only shows the name of existing files in this control (so this should work if your /some_path/main.cpp actually exists). I don't see any way to make GtkFileChooserButton show the file name if it doesn't exist, so I don't think this can be fixed, unfortunately.
Generally speaking, this control is really supposed to be used for loading existing files in GTK.
If you want to set it to a file, then you have to use SetFilename
wxFileName fn = "C:/src/wxWidgets.dev/samples/widgets/native.cpp";
filePicker->SetFileName(fn);

Qt create prototype tool for mobile device

Hi I need to create prototype tool for smartphone screen using Qt, I have png image of the smartphone right now and need to do place some text and image as shown in below image.
So my question is, is there anything similar already exist to create such a tool in C++ Qt.
Or do I need to use QPainter for achieving it.
Or any better method to accomplish it.
I can give you small code example how you can create such app. For main window you can set brush, created from this image, then set mask for window to ensure it painting right way. Also you need set Qt::FramelessWindowHint flag. Then you can add all needed widgets to this widget. Small example (assuming :/img/i.png is your picture, saved to resources):
At .h file:
#include <QFrame>
#include <QPixmap>
class TestFrame : public QFrame
{
Q_OBJECT
public:
TestFrame(QWidget* p = 0);
protected:
void resizeEvent(QResizeEvent* e) override;
private:
QPixmap _pix;
};
At .cpp file:
#include <QBitmap>
TestFrame::TestFrame(QWidget *p) :
QFrame(p, Qt::FramelessWindowHint | Qt::WindowSystemMenuHint),
_pix(":/img/i.png")
{
QPalette pal = palette();
pal.setBrush(QPalette::Background, QBrush(_pix));
setPalette(pal);
resize(_pix.width(), _pix.height());
}
void TestFrame::resizeEvent(QResizeEvent *e)
{
QFrame::resizeEvent(e);
setMask(_pix.scaled(size()).mask());
}
Also make sure your image has alpha-channel for invisible parts (I mean corners). For now it hasn't.

wxWidgets Seg fault on close

My files look something like so:
main.cpp
...
bool SyncApp::OnInit(){
SetTopWindow(new syncWindow(_("(S)FTP Automatic Sync")));
GetTopWindow()->Show(true);
return true;
}
...
syncwindow.h
#include <wx/wx.h>
class syncWindow : public wxFrame {
public:
syncWindow(wxString title) : wxFrame(NULL, -1, title) { initialize(); }
private:
void initialize();
wxTextCtrl * serverEntry;
};
syncwindow.cpp
void syncWindow::initialize(){
serverEntry = new wxTextCtrl(this, wxID_ANY);
this->AddChild(serverEntry);
}
For whatever reason whenever I close the window I get a segfault. If I don't add the serverEntry as a child to the window I don't get a segfault. I don't see why this is doing such a thing. I'm on CentOS 6, g++ 4.7 and wxGTK 2.8. Does anyone have any idea or a hint as to why this is happening?
Since you specified the parent window when constructing your child, the link is already present and calling this->AddChild(serverEntry); will cause double free or similar error when you close the window. http://docs.wxwidgets.org/2.8/wx_wxwindow.html#wxwindowaddchild
wxWindow::AddChild
Adds a child window. This is called automatically by window creation
functions so should not be required by the application programmer.
Notice that this function is mostly internal to wxWidgets and
shouldn't be called by the user code.

Connect not working properly while using QDialog as a popup

I am having a problem with connect in the program I am currently writing. I first create a "main window" dialog that contains buttons, line edits, etc. (which all work perfectly fine with my custom slots). One of the buttons (the "Add Class" button) should create a new pop up dialog that is a child of the mainWindow dialog. I wrote a new .h and .cpp for this new dialog (addClass.h and addClass.cpp). When I click the button, the dialog modality is set to ApplicationModal and up to this point, the code works; when I click "Add Class" and new dialog shows up as a pop up with all the labels, line edits, and buttons that I want. The problem comes in when I try and use the connect using this new class. Upon clicking the ok button the connect is not executed. The program compiles properly (using qmake and then make) and gives no errors during run time. I also took the .h and .cpp files from the pop up dialog and tested them with their own main.cpp and the connect worked perfectly. I am stumped as to what the problem could be, so any help would be awesome!
Here are some snipets of code that might be helpful:
the custom slot that initiates the pop up dialog in mainWindow.cpp (works and I include "addClass.h" in mainWindow.cpp):
void mainWindow::addClassCombo(){
addClass aC(win);
}
addClass.h:
#ifndef ADDCLASS_H
#define ADDCLASS_H
#include <QDialog>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QString>
class addClass : public QDialog{
Q_OBJECT
public:
addClass(QWidget *parent = 0);
private slots:
void addToTxt();
private:
QDialog *addMathClass;
QVBoxLayout *mainLayout;
QHBoxLayout *layoutOkCanc;
QLabel *nameL; //label for name of math class to be added
QLineEdit *name; //line edit for name
QPushButton *ok; //ok button
QPushButton *canc; //cancel button
};
#endif
addClass.cpp (works with its own main.cpp but not the one with my mainWindow.cpp):
#include <QtGui>
#include <QTextStream>
#include "addClass.h"
#include <iostream>
addClass::addClass(QWidget *parent):QDialog(parent){
addMathClass = new QDialog(parent);
mainLayout = new QVBoxLayout(addMathClass);
layoutOkCanc = new QHBoxLayout();
nameL = new QLabel("Math Class Name:");
name = new QLineEdit;
nameL->setBuddy(name);
ok = new QPushButton("Ok");
canc = new QPushButton("Cancel");
QObject::connect(canc, SIGNAL(clicked()), addMathClass, SLOT(close()) ); //<-works
QObject::connect(ok, SIGNAL(clicked()), this, SLOT(addToTxt()) ); //<-doesn't work
QObject::connect(ok, SIGNAL(clicked()), addMathClass, SLOT(close()) ); //<-works
layoutOkCanc->addStretch();
layoutOkCanc->addWidget(ok);
layoutOkCanc->addWidget(canc);
mainLayout->addWidget(nameL);
mainLayout->addWidget(name);
mainLayout->addLayout(layoutOkCanc);
addMathClass->setWindowModality(Qt::ApplicationModal);
addMathClass->setWindowTitle("Add Class");
addMathClass->show();
}
void addClass::addToTxt(){
std::cout<<"testing"<<std::endl;
}
Your addClass aC(win); goes out of scope and is being destroyed. Connection works, but after object destruction is disconnected. That's why you're not getting slot called

Objective-C++ / Cocoa attempt to create a window with button, not working?

I'm supposed to create a c++ class with c++ methods, that are made of objective-c and use cocoa, but now I've ran into a problem, and simply can't figure it out, cause I'm pretty new at objective-c. Also the point is that I should be able to create windows and buttons from c++. So when ever I build and run this program, it starts up but then instantly turns into a "not responding" state. Anyways, here's what I got:
Window.h
#ifndef WINDOW_H
#define WINDOW_H
class Window {
public:
Window();
void createButton();
};
#endif
Window.mm
#include "Window.h"
#include "Button.h"
Window::Window(){
NSWindow *window = [[NSWindow alloc]
initWithContentRect: NSMakeRect(100, 100, 200, 200)
styleMask: NSTitledWindowMask | NSMiniaturizableWindowMask
backing: NSBackingStoreBuffered
defer: NO];
[window setTitle: #"Test window"];
}
void Window::createButton(){
Button *button;
[[this contentView] addSubView: button];
// word this gives warning: no '-addSubView:' method found
// this is probably causing the problem I have
}
Button.h
class Button{
Button();
};
// There will be more methods here eventually
Button.mm
#include "Button.h"
Button::Button(){
NSButton *button = [[NSButton alloc]
initWithFrame: NSMakeRect(14, 100, 120, 40)];
[button setTitle: #"Hello world"];
[button setAction: #selector(invisible)];
[button setBezelStyle: NSRoundedBezelStyle];
[button setButtonType: NSMomentaryLightButton];
[button setBezelStyle: NSTexturedSquareBezelStyle];
}
Main.cpp
#include "Window.h"
#include "Button.h"
int main(int argc, char *argv[]){
Window x;
x.createButton();
}
So, does anyone have any idea why ain't it working, like I mentioned, I'm pretty new at Cocoa and Objective-C, still learning :P
And yeah, I've tried to fix it.
[[this contentView] addSubView: button];
As you suspect, that is causing your problem. "This" is not referring to the window; it refers to the class itself. (Remember, your class is not a subclass of an NSWindow)
One option is to declare your NSWindow in the header so it can be used globally. So make your header:
#ifndef WINDOW_H
#define WINDOW_H
class Window {
public:
Window();
void createButton();
NSWindow *window;
};
#endif
And then change the aforementioned line to:
[[window contentView] addSubview: button]; (Also notice to fix the capitalization on 'addSubview')
It's a similar problem with your button constructor. You are creating an NSButton, but that button is never seen or heard from again.
Your main function is not running the run loop, so the drawing and event handling systems will be non-responsive.
You never show the window you created.
I don't see where you save away the Cocoa objects so that your C++ API can manipulate them. For example, your Window constructor does not save the created window to a member variable, so you will have no way to manipulate that window after its creation.