How to draw multiple rectangles FLTK C++ - c++

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.

Related

How to make a point appear using fltk?

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();
}

Show main windows after the Splash Screen in C++ - QT

I have an application with a Splash Screen. I need to show the splash screen to appear before the main window appears and wait 3.5 seconds. Once it finished now I need to show the main window.
Here is the code what I have tried,
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QTimer>
#include "game.h"
#include "player.h"
#include <QSplashScreen>
#include <QObject>
Game *game;
int main(int argc, char *argv[])
{
//Splash screen
QApplication a(argc, argv);
QSplashScreen *splash = new QSplashScreen;
splash->setPixmap(QPixmap(":/images/Images/1.JPG"));
splash->show();
//main window
game = new Game();
QTimer::singleShot(3500, splash, SLOT(close()));
QTimer::singleShot(3500, game, SLOT(show()));
game->displayHome();
return a.exec();
}
Game Class
Game::Game(QWidget *parent)
{
scene = new QGraphicsScene();
scene->setSceneRect(0,0,800,600);
setBackgroundBrush(QBrush(QImage("img.png")));
setScene(scene);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setFixedSize(800,600);
show();
qDebug() << "yoyoyoy";
score = new Score();
health = new Health();
connect(this->health,SIGNAL(crash()),this,SLOT(gameover3()));
}
void Game::displayHome()
{
logo = new Logo();
scene->addItem(logo);
playButton = new Menubuttons(QString("Play"));
int bxPos = this->width()/2 - playButton->boundingRect().width()/2;
int byPos = 275;
playButton->setPos(bxPos,byPos);
connect(playButton,SIGNAL(clicked()),this,SLOT(startGame()));
scene->addItem(playButton);
instructions = new Menubuttons(QString("Instructions"));
int axPos = this->width()/2 - instructions->boundingRect().width()/2;
int ayPos = 350;
instructions->setPos(axPos,ayPos);
connect(instructions,SIGNAL(clicked()),this,SLOT(instruct()));
scene->addItem(instructions);
quitButton = new Menubuttons(QString("Quit"));
int qxPos = this->width()/2 - quitButton->boundingRect().width()/2;
int qyPos = 425;
quitButton->setPos(qxPos,qyPos);
connect(quitButton,SIGNAL(clicked()),this,SLOT(close()));
scene->addItem(quitButton);
scene->removeItem(inst);
}
But it appears both at the same time. How can I fix that?
The behavior you describe is not reproduced by the complete example below
#include <QtWidgets/QApplication>
#include <qmainwindow.h>
#include <qwidget.h>
#include <qtimer.h>
#include <qsplashscreen.h>
QMainWindow* game;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSplashScreen *splash = new QSplashScreen;
splash->setPixmap(QPixmap(":/StackOverflow/SplashScreen"));
splash->show();
game = new QMainWindow();
QTimer::singleShot(3500, splash, SLOT(close()));
QTimer::singleShot(3500, game, SLOT(show()));
return a.exec();
}
Running this displays a splash screen for 3.5 seconds followed by the main window. The issue may be your implementation for the Game class or the member function displayHome().
Edit
After your edit with the Game class definition and implementation it is clear the problem is calling show() at the end of the Game::Game() constructor. This causes game to display immediately upon construction, the subsequent call to show() in QTimer::singleShot(3500, game, SLOT(show())) is redundant upon the already visible object. To fix this simply remove show() from the Game constructor, i.e. it should be
Game::Game(QWidget *parent)
{
scene = new QGraphicsScene();
scene->setSceneRect(0, 0, 800, 600);
setBackgroundBrush(QBrush(QImage("img.png")));
setScene(scene);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setFixedSize(800, 600);
}

I can't change html text fontsize in Fl_Help_View

In this example when I change textsize(30) only increased the distance of lines
but not size
#include <FL/Fl.H>
#include <FL/fl_ask.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Help_View.H>
int main(int argc, char **argv) {
Fl_Window *win = new Fl_Window(640, 400, "Native File Chooser Test");
win->size_range(win->w(), win->h(), 0, 0);
win->begin();
Fl_Help_View *view = new Fl_Help_View(20,20,500, 200);
view->box(FL_FLAT_BOX);
view->color(win->color());
#define TAB "<Tab>"
view->textfont(FL_HELVETICA);
Fl_Help_Font_Style(FL_HELVETICA,30,FL_RED);
below is html text value
view->value("<h2 class="city">London</h2> <p>London is the capital of England.</p>");
view->textsize(30);
win->end();
win->show(argc, argv);
return(Fl::run());
}
Problem is connected with missed packages. I install libxft2 lib and now is ok.

how to use qt4 and irrlicht3d

i want to write a code for irrlicht begin scene in a qt4 window that have a button for exit i tried many times but every tim that's open two windows and i want only to run th scene and the button in one window
my code
#include <QApplication>
#include <QLabel>
#include <irrlicht.h>
#include <QApplication>
#include <QPushButton>
using namespace irr;
using namespace core;
using namespace video;
int main(int argc, char *argv[])
{
IrrlichtDevice* device = createDevice(EDT_OPENGL,dimension2d<u32>(640, 480), 16,false, false, false, 0);
if (!device)
return 1;
IVideoDriver* driver = device->getVideoDriver();
//qt4code
QApplication app(argc, argv);
QPushButton *button = new QPushButton(QString( "Qt/Irrlicht" ));
QObject::connect(button, SIGNAL(clicked()),&app, SLOT(quit()));
button->show();
app.exec();
//endcode
while (device->run())
{
driver->beginScene(true, true, SColor(255, 255, 255,255));
driver->endScene();
}
device->drop();
return 0;
}

alternating row colors in a QComboBox in mac and linux

I want to alternate the colors of a QComboBox. In Windows I have no problem using the view().setAlternatingRowColors(true) function. In Linux and Mac it looks like impossible. I tried also using style sheet (see following code) but I had the same kind of results (all rows with the same background color). Can you explain me what is my error?
#include <QtGui/QApplication>
#include <QComboBox>
#include <QAbstractItemView>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setStyleSheet("QComboBox QAbstractItemView{qproperty-alternatingRowColors: true;alternate-background-color: blue;background: red;}");
QComboBox b;
b.addItem("MM_NONE");
b.addItem("MM_VERT");
b.addItem("MM_FACE");
b.addItem("MM_EDGE");
bool tt = false;
tt = b.view()->alternatingRowColors();
b.show();
return a.exec();
}
At least on my box it appears that QPalette::Base and QPalette::AlternateBase are the same color :) Changing QPalette::AlternateBase to some other color makes this code work fine:
#include <QtGui/QApplication>
#include <QComboBox>
#include <QAbstractItemView>
#include <QPalette>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QComboBox b;
b.view()->setAlternatingRowColors(true);
QPalette p = b.palette();
p.setColor(QPalette::AlternateBase, Qt::red);
b.setPalette(p);
b.addItem("MM_NONE");
b.addItem("MM_VERT");
b.addItem("MM_FACE");
b.addItem("MM_EDGE");
b.show();
return a.exec();
}