I'm beginner in Qt/C++ programmation and i want to get screenshot of my window using Qt.
#include <qapplication.h>
#include <qpushbutton.h>
int main( int argc, char **argv )
{
QApplication a( argc, argv );
QPushButton hello( "Hello world!", 0 );
hello.resize( 100, 30 );
a.setMainWidget( &hello );
hello.show();
return a.exec();
}
Anyone can help me please ?
Here is a short way to take a screenshot of a specific window :
/* Take a screenshot of a window
Notice that window is a: QWidget *window; */
originalPixmap = QPixmap::grabWidget(window);
Related
I am using Ubuntu 20.04 and I have a small GTK 3.0 Form I am trying to get to maximize on first startup. I later want to get this into a Full GLADE project, but first I'm just trying to get this to maximize to full screen.
The commented sections are what I have tried to no avail.
Nothing works and it throws compiler errors, can anyone help me in my endeavor?
#include <gtk/gtk.h>
int main(int argc, char *argv[]) {
GtkWidget *window;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Center");
// gtk_window_set_default_size(GTK_WINDOW(window), 230, 150);
// gtk_window_fullscreen(GTK_WINDOW(window);
// gtk_window_fullscreen(GtkWindow *window);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_widget_show(window);
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_main();
return 0;
}
Thank you.
Use gtk_window_maximize(GtkWindow *window):
#include <gtk/gtk.h>
int main(int argc, char *argv[]) {
GtkWidget *window;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Center");
gtk_window_maximize(GTK_WINDOW(window));
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_widget_show(window);
//...
}
Note that you can call it before showing the window. Doc:
Asks to maximize window , so that it becomes full-screen. Note that you shouldn’t assume the window is definitely maximized afterward, because other entities (e.g. the user or window manager) could unmaximize it again, and not all window managers support maximization. But normally the window will end up maximized. Just don’t write code that crashes if not.
It’s permitted to call this function before showing a window, in which case the window will be maximized when it appears onscreen initially.
You can track maximization via the “window-state-event” signal on GtkWidget, or by listening to notifications on the “is-maximized” property.
Suggestion: Use gtkmm with C++
Example with gtkmm:
#include <gtkmm.h>
int main(int argc, char *argv[])
{
auto app =
Gtk::Application::create(argc, argv,
"org.gtkmm.maximize.base");
Gtk::Window window;
window.maximize();
return app->run(window);
}
I am trying to create a program in fltk and I followed this example
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>
int main(int argc, char **argv) {
Fl_Window *window = new Fl_Window(340,180);
Fl_Box *box = new Fl_Box(20,40,300,100,"Hello, World!");
box->box(FL_UP_BOX);
box->labelfont(FL_BOLD+FL_ITALIC);
box->labelsize(36);
box->labeltype(FL_SHADOW_LABEL);
window->end();
window->show(argc, argv);
return Fl::run();
}
from FLTK docs. The problem is I'm trying to draw multiple rectangle shapes to the window and it seem very tedious to create multiple boxed in order to have multiple rectangles. I tried looking up a lot of tutorials on drawing shapes on FLTK but I can't find anything simple enough to show me.
My code looks like this so far
#include <FL/Fl.H>
#include <FL/Fl_Widget.H>
#include <FL/Fl_Window.H>
// #include <FL/fl_draw.H>
#include <iostream>
int main() {
Fl_Window *window = new Fl_Window(900, 600);
window->position(0, 0);
window->color(FL_BLACK);
window->end();
window->show();
while (1) {
int ev = Fl::event();
if (ev == FL_SHORTCUT) {
if (Fl::event_key() == FL_Escape)
break;
}
Fl::check();
}
return 0;
}
and I would like to implement the drawing inside the loop (continuously).
As an important side note: You should really consider using Fl::run() instead of your custom while loop, I ran into many problems with a similar approach like yours.
Now, to answer your question:
Take the example and wrap the Box creation inside a loop. You can take for example an index for assigning different positions to each Box.
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>
int main(int argc, char **argv) {
Fl_Window *window = new Fl_Window(340,500);
// 3 Fl_Boxes in a loop
for (int i = 0; i < 3; ++i) {
Fl_Box *box = new Fl_Box(20, 40 + i*120, 300, 100,"Hello, World!");
box->box(FL_UP_BOX);
box->labelfont(FL_BOLD+FL_ITALIC);
box->labelsize(36);
box->labeltype(FL_SHADOW_LABEL);
}
window->end();
window->show(argc, argv);
return Fl::run();
}
You control the layout with the formula 20, 40 + i*120, 300, 100 in the box Constructor. You have to adjust that to your requirements.
With FLTK one basic rule is: All widgets you initialize between the window constructor and window->end() or any other widget that works the same way, for example Fl_Group, will become children of the surrounding element and will show, if their parent is shown.
I am not entirely sure, if Fl_Box is what you are looking for. I found Erco's FLTK Cheat Page extremely helpful. You might find inspiration and other approaches to your problem there.
As you can see from the screenshot, the button and widget are stretched. How to make them normal size? Here is the main code.
#include <gtkmm.h>
#include "w.h"
int main ( int argc, char **argv )
{
auto app = Gtk::Application::create ( argc, argv, "org.gtkmm.test" );
Gtk::Window window;
Gtk::Button btn;
Gtk::Box box(Gtk::ORIENTATION_HORIZONTAL);
W w;
btn.set_label ( "test" );
window.set_default_size ( 200, 200 );
box.pack_start ( btn, false, false, 0 );
box.pack_start ( w, false, false, 0 );
window.add ( box );
btn.show();
box.show();
w.show();
return app->run ( window );
}
After creating a window and drawing some shapes in it, I realized I cant make a point and just appear it on the window. I've searched the manual but I cant make anything out of it. Im using the fltk 1.3.0. How can I do it ?
Fltk comes with a bunch of example projects that are helpful. If you look at the line_style example you can easily reduce it to something drawing points like this:
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/fl_draw.H>
class TestWindow : public Fl_Window {
void draw()
{
fl_color(255, 0, 0);
fl_begin_points();
fl_point(50, 50);
fl_point(51, 51);
fl_end_points();
}
public:
TestWindow(int w, int h, const char *l = 0)
: Fl_Window(w, h, l) {}
};
int main(int argc, char ** argv) {
Fl_Window *window = new TestWindow(200, 200);
window->end();
window->show(argc, argv);
return Fl::run();
}
But just as a word of advice, drawing single points directly onto the window is rarely the smart thing to do. Drawing into images/buffers and then displaying them is the better alternative most of the time.
edit:
here's is an example of putting the drawing code in the main function.
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/fl_draw.H>
class TestWindow : public Fl_Window {
void draw() {}
public:
TestWindow(int w, int h, const char *l = 0) : Fl_Window(w, h, l) {}
};
int main(int argc, char ** argv) {
Fl_Window *window = new TestWindow(200, 200);
window->end();
window->show(argc, argv);
window->make_current();
fl_color(255, 0, 0);
fl_begin_points();
fl_point(50, 50);
fl_point(51, 51);
fl_end_points();
return Fl::run();
}
You should take notice of the disclaimer for make_current in the manual
Danger: incremental update is very hard to debug and maintain!
None of this is good practise beyond using it for simple exercises.
Based on the previous answer to this question, I found this in the documentation:
fl_begin_points()
Starts drawing a list of points.
Points are added to the list with fl_vertex()
So this is some code that shows some points (I added more to really see the points):
#include <FL/fl_draw.H>
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
class Drawing : public Fl_Window {
void draw(){
fl_begin_points();
//adding cushion to points to be able to see them.
//center at 10,10
fl_vertex(9,9);
fl_vertex(9,10);
fl_vertex(9,11);
fl_vertex(10,9);
fl_vertex(10,10);
fl_vertex(10,11);
fl_vertex(11,9);
fl_vertex(11,10);
fl_vertex(11,11);
fl_end_points();
fl_color(FL_BLACK);
}
public:
Drawing(int w, int h, const char *l = 0) : Fl_Window(w, h, l){}
};
int main(int argc, char **argv){
Fl_Window *window = new Drawing(340,180);
window->end();
window->show(argc, argv);
return Fl::run();
}
How can I:
1) properly set Gtk::Notebook in Gtk::HeaderBar like:
window.add(box);
2) implement autohide tabs ( animated like css transitions with scale(0) and scale(1) ) ?
My code:
#include <gtkmm.h>
int main(int argc, char *argv[])
{
Gtk::Main app(argc, argv);
Gtk::Window window;
window.set_default_size(200, 200);
Gtk::HBox box;
Gtk::HeaderBar titlebar;
Gtk::Notebook notebook;
Gtk::TextView text;
notebook.append_page(text, "First");
box.pack_start(notebook);
titlebar.set_custom_title(box);
window.set_titlebar(titlebar);
window.show_all_children();
return app.run(window);
}