I am getting very large values on the interface and where I am using Glib::DateTime::difference(), that function produces negative numbers
initializer of controller class:
UI_Controller::UI_Controller(Gtk::Builder * refference, Gtk::Application * app)
{
view_limit_time = new Gtk::Calendar();
view_limit_time->set_hexpand(true);
deffine_application (app);
this->refference = refference;
refference->get_widget("main_window",this->content_relations);
widgets = refference->get_objects();
Glib::ustring widget_names = "";
//dynamic_cast<Gtk::Button*>(button_access->get_child_at(1,0))
for (int i=0; i < widgets.size(); i++){
widget_names = widget_names+dynamic_cast<Gtk::Widget*>(widgets.at(i).get())->get_name()+"\n";
if (dynamic_cast<Gtk::Buildable*>(widgets.at(i).get())->get_name() == (Glib::ustring) "timer_grid"){
Gtk::Grid * button_access = dynamic_cast<Gtk::Grid*>(widgets.at(i).get());
grid_counter = i;
dynamic_cast<Gtk::Button*>(button_access->get_child_at(0,2))->signal_clicked().connect(sigc::bind<Gtk::Label*>(sigc::mem_fun(*this,&UI_Controller::start_timer),dynamic_cast<Gtk::Label*>(button_access->get_child_at(0,1)),i ) );
dynamic_cast<Gtk::Button*>(button_access->get_child_at(1,2))->signal_clicked().connect(sigc::bind(sigc::mem_fun(*this,&UI_Controller::stop_timer),i ) );
dynamic_cast<Gtk::Button*>(button_access->get_child_at(2,2))->signal_clicked().connect(sigc::bind(sigc::mem_fun(*this,&UI_Controller::restart_timer),i ) );
dynamic_cast<Gtk::Button*>(button_access->get_child_at(3,0))->signal_clicked().connect(sigc::bind<Gtk::Widget*>(sigc::mem_fun(*this,&UI_Controller::add_timer),button_access ) );
button_access->set_size_request(700,100);
//dynamic_cast<Gtk::ScrolledWindow*>((button_access->get_ancestor (GTK_TYPE_SCROLLED_WINDOW)))->set_min_content_height(300);
}
if (dynamic_cast<Gtk::Buildable*>(widgets.at(i).get())->get_name() == (Glib::ustring) "counter_grid"){
Gtk::Grid * button_access = dynamic_cast<Gtk::Grid*>(widgets.at(i).get());
button_access->attach(*(dynamic_cast<Gtk::Widget*>(view_limit_time)),0,1,2,1);
//view_limit_time->signal_day_selected().connect(sigc::bind<Gtk::Widget*>(sigc::mem_fun(*this,&UI_Controller::start_counter),(button_access->get_child_at(2,1)),i,(button_access->get_child_at(0,1))) );
grid_counter = i;
dynamic_cast<Gtk::Button*>(button_access->get_child_at(0,2))->signal_clicked().connect(sigc::bind<Gtk::Widget*>(sigc::mem_fun(*this,&UI_Controller::start_counter),(button_access->get_child_at(2,1)),i,(button_access->get_child_at(0,1))) );
dynamic_cast<Gtk::Button*>(button_access->get_child_at(1,2))->signal_clicked().connect(sigc::bind(sigc::mem_fun(*this,&UI_Controller::stop_counter),i ) );
dynamic_cast<Gtk::Button*>(button_access->get_child_at(2,2))->signal_clicked().connect(sigc::bind(sigc::mem_fun(*this,&UI_Controller::restart_counter),i ) );
dynamic_cast<Gtk::Button*>(button_access->get_child_at(3,0))->signal_clicked().connect(sigc::bind<Gtk::Widget*>(sigc::mem_fun(*this,&UI_Controller::add_counter),button_access ) );
button_access->set_size_request(700,300);
//dynamic_cast<Gtk::ScrolledWindow*>((button_access->get_ancestor (GTK_TYPE_SCROLLED_WINDOW)))->set_min_content_height(300);
}
}
app->run();
}
header from the controller:
#ifndef _U_I_CONTROLLER_H_
#define _U_I_CONTROLLER_H_
#include <unistd.h>
#include <gtkmm.h>
#include <unordered_map>
#include <glibmm/datetime.h>
#include <time-keeper.h>
#include <iostream>
#define TIMER_GRID_UI "src/n_time.ui"
#define COUNTER_GRID_UI "src/n_counter.ui"
namespace std{
template <>
struct hash<Time_Keeper>
{
size_t operator()( Time_Keeper& t) const
{
return t.hasheable().hash();
}
};
}
class UI_Controller
{
public:
UI_Controller(Gtk::Builder* refference,Gtk::Application * app);
void deffine_application(Gtk::Application * app);
void add_window_to_application(Gtk::Window * window);
protected:
private:
int grid_counter;
bool timer_started=false;
Gtk::Builder * refference;
Gtk::ApplicationWindow * content_relations;
Gtk::Application * app;
Gtk::Calendar * view_limit_time;
std::vector<Glib::RefPtr<Glib::Object>> widgets;
std::unordered_map<int,Time_Keeper> bind_time;
void show_window(Gtk::Window *window);
void start_timer(Gtk::Label * selected, int position);
void stop_timer(int i) { (bind_time[i]).stop_timer (); };
void restart_timer(int i) { (bind_time[i]).reset_timer ();};
void add_timer(Gtk::Widget * selected);
bool timeout_timer(Gtk::Label * display,int position);
bool timeout_counter(Gtk::Label * display,int position, Glib::DateTime when);
int get_index(Glib::RefPtr<Glib::Object> target);
void start_counter (Gtk::Widget * selected, int position, Gtk::Widget * set_when);
void stop_counter (int i){ (bind_time[i]).stop_counter();};
void restart_counter(int i){ return ;};
void add_counter(Gtk::Widget * selected);
/*sigc::connection Glib::SignalTimeout::connect(const sigc::slot<bool()>& slot,
unsigned int interval, int priority = Glib::PRIORITY_DEFAULT);*/
};
#endif // _U_I_CONTROLLER_H_
the actionable function related to the counter:
void UI_Controller::start_counter(Gtk::Widget * selected, int position, Gtk::Widget * set_when){
guint selected_year, selected_month, selected_day;
if (bind_time.find(position) == bind_time.end() ){
bind_time [position] = *(new Time_Keeper());
}
//Glib::DateTime when = ( Glib::DateTime::create_from_iso8601(( (dynamic_cast<Gtk::Entry*>(set_when))->get_text())) );
(dynamic_cast<Gtk::Calendar*>(set_when))->get_date(selected_year,selected_month,selected_day);
Glib::DateTime when = (Glib::DateTime::create_utc(selected_year,selected_month,selected_day,1,1,1));
//refference->get_widget("timer_display",display);
//if (!timer_started){
sigc::slot<bool()> my_slot = sigc::bind(sigc::mem_fun(*this,
&UI_Controller::timeout_counter), (dynamic_cast<Gtk::Label*>(selected)), position,when);
auto conn = Glib::signal_timeout().connect(my_slot, 1000);
timer_started = true;
//}
}
the function calculating the time difference on the Time_Keeper class:
Glib::ustring Time_Keeper::display_counter(Glib::DateTime when){
counter_active = true;
Glib::DateTime now = Glib::DateTime::create_now_utc();
size_t seconds = ( ((size_t)(when.difference(now))) /1000000);
std::cout << now.get_year() << std::endl;
size_t hours = (seconds/3600);
seconds%=3600;
size_t minutes=seconds/60;
seconds%=60;
return ((Glib::ustring) ( std::to_string(hours)+":" \
+std::to_string(minutes)+":" \
+std::to_string(seconds) ));
}
ui file:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkApplicationWindow" id="main_window">
<property name="can-focus">False</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">13</property>
<property name="homogeneous">True</property>
<child>
<object class="GtkScrolledWindow">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="shadow-type">in</property>
<child>
<object class="GtkViewport">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">current activity time</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<!-- n-columns=4 n-rows=3 -->
<object class="GtkGrid" id="timer_grid">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<child>
<object class="GtkEntry">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="hexpand">True</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">0</property>
<property name="width">3</property>
</packing>
</child>
<child>
<object class="GtkButton" id="start_timer">
<property name="label" translatable="yes">Start</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="stop_timer">
<property name="label" translatable="yes">Stop</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="restart_timer">
<property name="label" translatable="yes">Restart</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">2</property>
<property name="top-attach">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="add_timer">
<property name="label" translatable="yes">+</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">3</property>
<property name="top-attach">0</property>
<property name="height">3</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="timer_display">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">1</property>
<property name="width">3</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkScrolledWindow">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="shadow-type">in</property>
<child>
<object class="GtkViewport">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">activity limit</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<!-- n-columns=4 n-rows=3 -->
<object class="GtkGrid" id="counter_grid">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<child>
<object class="GtkEntry">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="hexpand">True</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">0</property>
<property name="width">3</property>
</packing>
</child>
<child>
<object class="GtkButton">
<property name="label" translatable="yes">Start</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">2</property>
</packing>
</child>
<child>
<object class="GtkButton">
<property name="label" translatable="yes">Stop</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">2</property>
</packing>
</child>
<child>
<object class="GtkButton">
<property name="label" translatable="yes">Restart</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">2</property>
<property name="top-attach">2</property>
</packing>
</child>
<child>
<object class="GtkButton">
<property name="label" translatable="yes">+</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">3</property>
<property name="top-attach">0</property>
<property name="height">3</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
</object>
<packing>
<property name="left-attach">2</property>
<property name="top-attach">1</property>
</packing>
</child>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
on the line (dynamic_cast<Gtk::Calendar*>(set_when))->get_date(selected_year,selected_month,selected_day); the month was being set to 1 unity less and that was confusing the calculation of the time difference. I have found it when outputting the difference of the function get_day_of_year from the Glib::DateTime objects "now" and "when" and seeing that it was showing high values
Related
I used script mediator to make multi where condition ,i have a method get the script is correct when i get from query parama as below :
<script language="js"><![CDATA[var queryString = "WHERE ";
var AND =" AND ";
var id = mc.getProperty('query.param.id');
var age = mc.getProperty('query.param.age');
var note = mc.getProperty('query.param.note');
queryString += (id)? " id=" + id+" " + AND : " ";
queryString += (age)? " age=" + age+" " + AND : " ";
queryString += (note)? " note=" + note+" " + AND : "";
queryString =queryString.toString();
var n = queryString.lastIndexOf(AND);
mc.setProperty('queryString',queryString.slice(0, n));]]></script>
<property expression="get-property('queryString')" name="uri.var.queryString" scope="default" type="STRING"/>
<call>
<endpoint>
<http method="get" uri-template="http://ITMTNF3RRJSJ:8290/services/studenttest/readstudent?filtreQuery={uri.var.queryString}">
But now i have a method put with multi where i tried get from payload json then i used the iterate mediator to do the update according to the list of objects but it doesnt't work
here is my code:
<resource methods="PUT" uri-template="modif">
<inSequence>
<property expression="json-eval($)" name="JSONPayload" scope="default" type="STRING"/>
<script language="js"><![CDATA[var queryString = "WHERE ";
var AND =" AND ";
var pl_string = mc.getProperty("JSONPayload");
var newPayload="{\"event\": " + pl_string + "}";
queryString += (id)? " id=" + id+" " + AND : " ";
queryString += (age)?" age=" + age+" " + AND : " ";
queryString += (note)? " note=" + note+" " + AND : "";
queryString =queryString.toString();
var n = queryString.lastIndexOf(AND);
mc.setPayloadJSON(newPayload);]]></script>
<property expression="get-property('queryString')" name="uri.var.queryString" scope="default" type="STRING"/>
<log level="custom">
<property expression="get-property('JSONPayload')" name="msgg"/>
</log>
<call>
<endpoint>
<http method="get" uri-template="http://ITMTNF3RRJSJ:8290/services/studenttest/readstudent?filtreQuery={uri.var.queryString}">
<suspendOnFailure>
<initialDuration>-1</initialDuration>
<progressionFactor>-1</progressionFactor>
<maximumDuration>0</maximumDuration>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
</markForSuspension>
</http>
</endpoint>
</call>
<iterate expression="//etuidants/etudiant">
<target>
<sequence>
<payloadFactory media-type="json">
<format>{
"_putmodifstudent": {
"id":${payload.etudiant.id},
"nom":"${payload.etudiant.nom}",
"prenom":"${payload.etudiant.prenom}",
"age":"${payload.etudiant.age}",
"note":"${payloadetudiant.note}",
}
}</format>
<args/>
</payloadFactory>
<call>
<endpoint>
<http method="put" statistics="enable" trace="enable" uri-template="http://ITMTNF3RRJSJ:8290/services/studenttest/modifstudent">
I have SQL Update statement with optional filter (multi where condition ) that i would like to exposed as an
Api
the problem is that I need to put where parameters optional :
for example the user will be able to search by id only or by id and age or by id and age and note
tried using the mediator script
<resource methods="PUT" uri-template="modif">
<inSequence>
<script language="js"><![CDATA[var queryString = "WHERE ";
var AND =" AND ";
var age = mc.getProperty('query.param.age');
var note = mc.getProperty('query.param.note');
queryString += (age)? " age=" + age+" " + AND : " ";
queryString += (note)? " note=" + note+" " + AND : "";
queryString =queryString.toString();
var n = queryString.lastIndexOf(AND);
mc.setProperty('queryString',queryString.slice(0, n));]]></script>
<property expression="get-property('queryString')" name="uri.var.queryString" scope="default" type="STRING"/>
<payloadFactory media-type="json">
<format>{
"_putmodifstudent": {
"id" : $id,
"nom": "$nom",
"prenom:"$prenom",
"whereage":$age,
"wherenote":"$note"
}
}</format>
<args/>
</payloadFactory>
<call>
<endpoint>
<http method="put" statistics="enable" trace="enable" uri-template="http://ITMTNF3RRJSJ:8290/services/studenttest/modifstudent">
<suspendOnFailure>
<initialDuration>-1</initialDuration>
<progressionFactor>-1</progressionFactor>
<maximumDuration>0</maximumDuration>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
</markForSuspension>
</http>
</endpoint>
</call>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
My sql statement in dataservice
<sql>UPDATE etudiant SET nom =?, prenom =?, age =?, note =? where id =? and age =? and note =?</sql>
I can't use Gtk::Adjustment widget from my glade file. The program builds and runs with the following error:
(test-glade-spinbutton:227780): gtkmm-CRITICAL **: 13:38:45.769: gtkmm: object `adjustment_width' (type=`gtkmm__GtkAdjustment') (in GtkBuilder file) is not a widget type.
(test-glade-spinbutton:227780): gtkmm-CRITICAL **: 13:38:45.769: gtkmm: Gtk::Builder: widget `adjustment_width' was not found in the GtkBuilder file, or the specified part of it.
** (test-glade-spinbutton:227780): CRITICAL **: 13:38:45.769: Gtk::Builder::get_widget(): dynamic_cast<> failed.
However, for this post I've removed interaction with Gtk::Adjustment so the program can actually run. The program crashes if I try to read my adjustment widget's current value.
Below is the C++ code:
#include <gtkmm.h>
using Gtk::Adjustment;
using Gtk::Application;
using Gtk::Builder;
using Gtk::Grid;
using Gtk::SpinButton;
using Gtk::Window;
class MyWindow : public Window
{
Grid *main_content;
Adjustment *adjustment_width;
SpinButton *width;
public:
MyWindow()
{
auto builder = Builder::create_from_file("test-spinbutton.glade");
builder->get_widget("main_content" , main_content);
builder->get_widget("adjustment_width", adjustment_width);
builder->get_widget("width" , width);
add(*main_content);
present();
}
};
int main()
{
auto app = Application::create("domain.reverse.test-spinbutton");
MyWindow hello;
return app->run(hello);
}
and the glade file:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.2 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkAdjustment" id="adjustment_height">
<property name="lower">1</property>
<property name="upper">2147483647</property>
<property name="value">1</property>
<property name="step_increment">1</property>
<property name="page_increment">10</property>
</object>
<object class="GtkAdjustment" id="adjustment_width">
<property name="lower">1</property>
<property name="upper">2147483647</property>
<property name="value">1</property>
<property name="step_increment">1</property>
<property name="page_increment">10</property>
</object>
<object class="GtkGrid" id="main_content">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">24</property>
<property name="margin_top">6</property>
<property name="label" translatable="yes">Width</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">24</property>
<property name="margin_top">6</property>
<property name="label" translatable="yes">Height</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">2</property>
</packing>
</child>
<child>
<object class="GtkSpinButton" id="width">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="margin_left">6</property>
<property name="margin_top">6</property>
<property name="hexpand">True</property>
<property name="adjustment">adjustment_width</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkSpinButton" id="height">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="margin_left">6</property>
<property name="margin_top">6</property>
<property name="hexpand">True</property>
<property name="adjustment">adjustment_height</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">2</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Image Size</property>
<attributes>
<attribute name="weight" value="bold"/>
</attributes>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkToggleButton" id="toggle_ratio_wh">
<property name="label">gtk-execute</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="margin_left">6</property>
<property name="margin_top">6</property>
<property name="use_stock">True</property>
<property name="always_show_image">True</property>
</object>
<packing>
<property name="left_attach">2</property>
<property name="top_attach">1</property>
<property name="height">2</property>
</packing>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<style>
<class name="dialog-main-content"/>
</style>
</object>
</interface>
The error says that adjustment_width is not a widget, like its inheritance tree suggest (unlike a grid or a spin button) so get_widget won't work. You need to use get_object. For example:
#include <iostream>
#include <gtkmm.h>
using Gtk::Adjustment;
using Gtk::Application;
using Gtk::Builder;
using Gtk::Grid;
using Gtk::SpinButton;
using Gtk::Window;
class MyWindow : public Window
{
Grid *main_content;
SpinButton *width;
Glib::RefPtr<Adjustment> adjustment_width;
public:
MyWindow()
{
auto builder = Builder::create_from_file("test-spinbutton.glade");
// Widgets:
builder->get_widget("main_content" , main_content);
builder->get_widget("width" , width);
// Other objects (non widgets):
// 1. Get the object from the object three:
Glib::RefPtr<Glib::Object> adjustmentObject = builder->get_object("adjustment_width");
// 2. At this point, it is a Glib::Object (which is Adjustment's base type), so we need
// to cast:
adjustment_width = Glib::RefPtr<Adjustment>::cast_dynamic(adjustmentObject);
// 3. Then we can access it normally:
std::cout << "The adjustment value is : " << adjustment_width->get_value() << std::endl;
add(*main_content);
present();
}
};
int main()
{
auto app = Application::create("domain.reverse.test-spinbutton");
MyWindow hello;
return app->run(hello);
}
I tried to get any key using keyPressEvent() function, but I found out in debug mode that this function is called only when the key "shift/alt/ctrl" is pressed. I tried to press the key in textEdit box.
tipmanager.h :
namespace Ui {
class TipManager;
}
class TipManager : public QWidget
{
Q_OBJECT
public:
explicit TipManager(QWidget *parent = 0);
~TipManager();
...
protected:
virtual void keyPressEvent(QKeyEvent*);
...
private:
void on_titleEdit_textChanged(const QString &title);
void on_codeTextEdit_textChanged();
void on_memoTextEdit_textChanged();
void enableButtons();
...
private:
Ui::TipManager *ui;
...
QDirModel *model;
};
#endif // TIPMANAGER_H
tipmanager.cpp :
#include "tipmanager.h"
#include "ui_tipmanager.h"
TipManager::TipManager(QWidget *parent) :
QWidget(parent),
ui(new Ui::TipManager)
{
ui->setupUi(this);
ui->dirEdit->setReadOnly(true);
...
}
...
void TipManager::enableButtons()
{
//disable new button when all text boxes are empty
flagTitleEdit|flagCodeTextEdit|flagMemoTextEdit
? ui->newButton->setEnabled(true)
: ui->newButton->setEnabled(false);
//enable save button when only both title and codeText bit are true
flagTitleEdit&flagCodeTextEdit
? ui->saveButton->setEnabled(true)
: ui->saveButton->setEnabled(false);
//enable edit button when even a single text box is not empty
flagTitleEdit|flagCodeTextEdit|flagMemoTextEdit
? ui->editButton->setEnabled(true)
: ui->editButton->setEnabled(false);
}
void TipManager::on_titleEdit_textChanged(const QString &title)
{
//when title text is empty, title bit is false
QString text = title;
text.trimmed().isEmpty() ? flagTitleEdit = false
: flagTitleEdit = true;
//memo title edit is modified
flagModified = true;
enableButtons();
}
void TipManager::on_codeTextEdit_textChanged()
{
//when code text is empty, codeText bit is false
QString text = ui->codeTextEdit->toPlainText();
text.trimmed().isEmpty() ? flagCodeTextEdit = false
: flagCodeTextEdit = true;
//code text edit is modified
flagModified = true;
enableButtons();
}
void TipManager::on_memoTextEdit_textChanged()
{
//when memo text is empty, memoText bit is false
QString text = ui->memoTextEdit->toPlainText();
text.trimmed().isEmpty() ? flagMemoTextEdit = false
: flagMemoTextEdit = true;
//memo text edit is modified
flagModified = true;
enableButtons();
}
void TipManager::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Tab)
{
}
}
tipmanager.ui :
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>TipManager</class>
<widget class="QWidget" name="TipManager">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>900</width>
<height>558</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>720</width>
<height>0</height>
</size>
</property>
<property name="windowTitle">
<string>TipManager</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="1">
<widget class="Line" name="line">
<property name="enabled">
<bool>true</bool>
</property>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QWidget" name="RightLayout" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QVBoxLayout" name="rightLayout">
<property name="spacing">
<number>10</number>
</property>
<property name="sizeConstraint">
<enum>QLayout::SetMinimumSize</enum>
</property>
<property name="leftMargin">
<number>1</number>
</property>
<item>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QLabel" name="label_memo">
<property name="text">
<string>Memo</string>
</property>
</widget>
</item>
<item>
<widget class="QTextEdit" name="memoTextEdit">
<property name="enabled">
<bool>true</bool>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="cursor" stdset="0">
<cursorShape>IBeamCursor</cursorShape>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="label_file">
<property name="text">
<string>File</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="dirEdit">
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="cursor">
<cursorShape>ArrowCursor</cursorShape>
</property>
<property name="mouseTracking">
<bool>false</bool>
</property>
<property name="acceptDrops">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QTreeView" name="treeView">
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="cursor" stdset="0">
<cursorShape>ArrowCursor</cursorShape>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="newButton">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>New</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="editButton">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Edit</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="searchButton">
<property name="text">
<string>Search</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QPushButton" name="loadButton">
<property name="text">
<string>Load Dir</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="saveButton">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Save</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="aboutButton">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>About</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item row="0" column="0">
<widget class="QWidget" name="LeftLayout" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>550</width>
<height>480</height>
</size>
</property>
<layout class="QVBoxLayout" name="leftLayout">
<property name="spacing">
<number>10</number>
</property>
<property name="sizeConstraint">
<enum>QLayout::SetMinAndMaxSize</enum>
</property>
<property name="leftMargin">
<number>1</number>
</property>
<property name="rightMargin">
<number>1</number>
</property>
<item>
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Title</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="titleEdit">
<property name="enabled">
<bool>true</bool>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_5">
<item>
<widget class="QLabel" name="label_code">
<property name="text">
<string>Code</string>
</property>
</widget>
</item>
<item>
<widget class="QTextEdit" name="codeTextEdit">
<property name="enabled">
<bool>true</bool>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="cursor" stdset="0">
<cursorShape>IBeamCursor</cursorShape>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
I declared #include <QKeyEvent> of course, and also wrote setFocusPolicy() but it didn't help.
I'm using UI with a dialog, so not in the case of MainWindows.
I want to know why this detects only shift/alt/ctrl key.
I'm using Qt 5.3.
If you want to capture the keys when you click on the codeTextEdit you must implement an event filter as shown below:
tipmanager.h
#ifndef TIPMANAGER_H
#define TIPMANAGER_H
#include <QWidget>
namespace Ui {
class TipManager;
}
class TipManager : public QWidget
{
Q_OBJECT
public:
explicit TipManager(QWidget *parent = 0);
~TipManager();
bool eventFilter(QObject *watched, QEvent *event);
private:
Ui::TipManager *ui;
};
#endif // TIPMANAGER_H
tipmanager.cpp
#include "tipmanager.h"
#include "ui_tipmanager.h"
#include <QKeyEvent>
#include <QTextEdit>
#include <QDebug>
TipManager::TipManager(QWidget *parent) :
QWidget(parent),
ui(new Ui::TipManager)
{
ui->setupUi(this);
ui->codeTextEdit->installEventFilter(this);
}
TipManager::~TipManager()
{
delete ui;
}
bool TipManager::eventFilter(QObject *watched, QEvent *event)
{
if(watched == ui->codeTextEdit && event->type() == QEvent::KeyPress){
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
qDebug()<<keyEvent->key();
}
return QWidget::eventFilter(watched, event);
}
I've spent couple days on doing this, but every solution that I tried just didn't work.
I have colorPicker where I pick a color. The ColorPicker is a class. I have button in another class called ToolWindow. I want to change color of button in class ToolWindow after picking a color in class ColorPicker.
Here is some code:
toolWindow.h
#ifndef TOOLWINDOW_H
#define TOOLWINDOW_H
#include <QDialog>
#include "colorpicker.h"
//#include "mainwindow.h"
namespace Ui {
class ToolWindow;
}
class ToolWindow : public QDialog
{
Q_OBJECT
public:
explicit ToolWindow(QWidget *parent = 0);
~ToolWindow();
void setColor(QColor color);
private slots:
void on_penTool_clicked();
void on_rectTool_clicked();
void on_selectTool_clicked();
void on_circleTool_clicked();
void on_lineTool_clicked();
void on_colorButton_clicked();
private:
Ui::ToolWindow *ui;
ColorPicker colorPicker;
QColor nColor;
};
#endif // TOOLWINDOW_H
toolWindow.cpp
#include "toolwindow.h"
#include "ui_toolwindow.h"
#include "colorpicker.h"
#include "mainwindow.h"
#include <QMouseEvent>
ToolWindow::ToolWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::ToolWindow)
{
ui->setupUi(this);
}
ToolWindow::~ToolWindow()
{
delete ui;
}
void ToolWindow::on_penTool_clicked()
{
mSelection = 1;
}
void ToolWindow::on_rectTool_clicked()
{
mSelection = 2;
}
void ToolWindow::on_selectTool_clicked()
{
mSelection = 0;
}
void ToolWindow::on_circleTool_clicked()
{
mSelection = 3;
}
void ToolWindow::on_lineTool_clicked()
{
mSelection = 4;
}
void ToolWindow::on_colorButton_clicked()
{
colorPicker.show();
}
void ToolWindow::setColor(QColor color)
{
//ui->colorButton->setPalette(color);
//nColor = color;
}
colorPicker.h
#ifndef COLORPICKER_H
#define COLORPICKER_H
#include <QDialog>
#include <QColor>
//#include "mainwindow.h"
namespace Ui {
class ColorPicker;
}
class ColorPicker : public QDialog
{
Q_OBJECT
public:
explicit ColorPicker(QWidget *parent = 0);
~ColorPicker();
QPalette palete;
private slots:
void on_buttonBox_rejected();
void onColorChanged();
void onCMYKChanged();
void on_buttonBox_accepted();
private:
Ui::ColorPicker *ui;
double min(double a,double b,double c);
QColor mColor;
double k,c,m,y;
int r,g,b;
signals:
void colorChanged(QColor arg);
};
#endif // COLORPICKER_H
colorPicker.cpp
#include "colorpicker.h"
#include "ui_colorpicker.h"
#include "toolwindow.h"
#include "mainwindow.h"
#include <QPalette>
ColorPicker::ColorPicker(QWidget *parent) :
QDialog(parent),
ui(new Ui::ColorPicker)
{
ui->setupUi(this);
connect(ui->redSlider,SIGNAL(valueChanged(int)),SLOT(onColorChanged()));
connect(ui->greenSlider,SIGNAL(valueChanged(int)),SLOT(onColorChanged()));
connect(ui->blueSlider,SIGNAL(valueChanged(int)),SLOT(onColorChanged()));
connect(ui->blackSlider,SIGNAL(valueChanged(int)),SLOT(onCMYKChanged()));
connect(ui->yellowSlider,SIGNAL(valueChanged(int)),SLOT(onCMYKChanged()));
connect(ui->magentaSlider,SIGNAL(valueChanged(int)),SLOT(onCMYKChanged()));
connect(ui->cyanSlider,SIGNAL(valueChanged(int)),SLOT(onCMYKChanged()));
//connect(this,SIGNAL(colorChanged(QColor)),toolWindow,SLOT(setColor(QColor)));
onColorChanged();
}
ColorPicker::~ColorPicker()
{
delete ui;
}
void ColorPicker::on_buttonBox_rejected()
{
this->close();
}
double ColorPicker::min(double a,double b,double c)
{
double tmp;
if(a>b)
{
if(a>c)
tmp = a;
else
tmp = c;
}
else
{
if(b>c)
tmp = b;
else
tmp = c;
}
return tmp;
}
void ColorPicker::onColorChanged()
{
int q1,q2,q3;
mColor.setRgb(ui->redSlider->value(),ui->greenSlider->value(),ui->blueSlider->value());
q1=ui->redSlider->value();
q2=ui->greenSlider->value();
q3=ui->blueSlider->value();
palete = ui->widget->palette();
palete.setColor(QPalette::Window, mColor);
ui->widget->setPalette(palete);
emit colorChanged(mColor);
k=min(q1,q2,q3);
c=(1-ui->redSlider->value()-k)/(1-k);
m=(1-ui->greenSlider->value()-k)/(1-k);
y=(1-ui->blueSlider->value()-k)/(1-k);
ui->blackSlider->setValue(k);
ui->cyanSlider->setValue(c);
ui->magentaSlider->setValue(m);
ui->yellowSlider->setValue(y);
}
void ColorPicker::onCMYKChanged()
{
mColor.setRgb(ui->redSlider->value(),ui->greenSlider->value(),ui->blueSlider->value());
palete = ui->widget->palette();
palete.setColor(QPalette::Window, mColor);
ui->widget->setPalette(palete);
emit colorChanged(mColor);
}
void ColorPicker::on_buttonBox_accepted()
{
//QPalette pal = ui->widget->palette();
//pal.setColor(QPalette::Window, mColor);
//Ui::ToolWindow
//MainWindow::MainWindow().mColor.setRgb(ui->redSlider->value(),ui->greenSlider->value(),ui->blueSlider->value());
//mColor.setRgb(ui->redSlider->value(),ui->greenSlider->value(),ui->blueSlider->value());
}
mainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "toolwindow.h"
namespace Ui {
class MainWindow;
}
class QPainter;
class QImage;
extern int mSelection;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
ToolWindow toolWindow;
QColor mColor;
protected:
void paintEvent(QPaintEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
private:
Ui::MainWindow *ui;
QPainter *mPainter;
QImage *mImage;
QPixmap mPix;
QPoint mBegin;
QPoint mEnd;
QRect mRect;
QLine mLine;
bool drawStarted;
bool mPressed;
int mSize;
};
#endif // MAINWINDOW_H
mainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDesktopWidget>
#include <QMouseEvent>
#include <QPaintEvent>
#include <QPainter>
#include <QImage>
int mSelection = 0;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
mPix = QPixmap(400,400);
mPix.fill(Qt::white);
mPressed = false;
drawStarted = false;
mColor = QColor(Qt::black);
mSize = 2;
toolWindow.setWindowFlags(Qt::SubWindow | Qt::WindowDoesNotAcceptFocus);
toolWindow.show();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.drawPixmap(0,0,mPix);
QPen pen(mColor);
pen.setCapStyle(Qt::RoundCap);
pen.setWidth(mSize);
painter.setPen(pen);
if(mPressed)
{
if(mSelection == 2)
painter.drawRect(mRect);
else if(mSelection == 4) {
painter.drawLine(mLine);
}
drawStarted = true;
} else if (drawStarted){
QPainter tempPainter(&mPix);
tempPainter.setPen(pen);
if(mSelection == 2)
tempPainter.drawRect(mRect);
else if(mSelection == 4) {
tempPainter.drawLine(mLine);
}
painter.drawPixmap(0,0,mPix);
}
painter.end();
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
mPressed = true;
mBegin = event->pos();
if(mSelection == 2){
mRect.setTopLeft(event->pos());
mRect.setBottomRight(event->pos());
}
else if(mSelection == 4){
mLine.setP1(event->pos());
mLine.setP2(event->pos());
}
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
QPen pen(mColor);
pen.setCapStyle(Qt::RoundCap);
pen.setWidth(mSize);
switch(mSelection)
{
case 1:
{
QPainter penPainter(&mPix);
penPainter.setPen(pen);
mEnd = event->pos();
penPainter.drawLine(mBegin, mEnd);
mBegin = mEnd;
update();
break;
}
case 2:
{
mRect.setBottomRight(event->pos());
update();
break;
}
case 4:
{
mLine.setP2(event->pos());
update();
break;
}
}
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
mPressed = false;
update();
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setStyle("fusion");
MainWindow w;
w.show();
return a.exec();
}
colorPicker.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ColorPicker</class>
<widget class="QDialog" name="ColorPicker">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>355</width>
<height>293</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>9</x>
<y>9</y>
<width>16</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>R</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>10</x>
<y>40</y>
<width>16</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>G</string>
</property>
</widget>
<widget class="QLabel" name="label_3">
<property name="geometry">
<rect>
<x>10</x>
<y>70</y>
<width>16</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>B</string>
</property>
</widget>
<widget class="QSlider" name="redSlider">
<property name="geometry">
<rect>
<x>30</x>
<y>10</y>
<width>131</width>
<height>22</height>
</rect>
</property>
<property name="maximum">
<number>255</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
<widget class="QSlider" name="greenSlider">
<property name="geometry">
<rect>
<x>30</x>
<y>40</y>
<width>131</width>
<height>22</height>
</rect>
</property>
<property name="maximum">
<number>255</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
<widget class="QSlider" name="blueSlider">
<property name="geometry">
<rect>
<x>30</x>
<y>70</y>
<width>131</width>
<height>22</height>
</rect>
</property>
<property name="maximum">
<number>255</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
<widget class="QSpinBox" name="redSpinbox">
<property name="geometry">
<rect>
<x>170</x>
<y>8</y>
<width>51</width>
<height>20</height>
</rect>
</property>
<property name="maximum">
<number>255</number>
</property>
</widget>
<widget class="QSpinBox" name="greenSpinbox">
<property name="geometry">
<rect>
<x>170</x>
<y>40</y>
<width>51</width>
<height>20</height>
</rect>
</property>
<property name="maximum">
<number>255</number>
</property>
</widget>
<widget class="QSpinBox" name="blueSpinbox">
<property name="geometry">
<rect>
<x>170</x>
<y>70</y>
<width>51</width>
<height>20</height>
</rect>
</property>
<property name="maximum">
<number>255</number>
</property>
</widget>
<widget class="QLabel" name="label_4">
<property name="geometry">
<rect>
<x>10</x>
<y>130</y>
<width>16</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>K</string>
</property>
</widget>
<widget class="QLabel" name="label_5">
<property name="geometry">
<rect>
<x>10</x>
<y>160</y>
<width>16</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>Y</string>
</property>
</widget>
<widget class="QLabel" name="label_6">
<property name="geometry">
<rect>
<x>10</x>
<y>190</y>
<width>16</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>M</string>
</property>
</widget>
<widget class="QSlider" name="blackSlider">
<property name="geometry">
<rect>
<x>30</x>
<y>130</y>
<width>131</width>
<height>22</height>
</rect>
</property>
<property name="maximum">
<number>255</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
<widget class="QSlider" name="yellowSlider">
<property name="geometry">
<rect>
<x>30</x>
<y>160</y>
<width>131</width>
<height>22</height>
</rect>
</property>
<property name="maximum">
<number>255</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
<widget class="QSlider" name="magentaSlider">
<property name="geometry">
<rect>
<x>30</x>
<y>190</y>
<width>131</width>
<height>22</height>
</rect>
</property>
<property name="maximum">
<number>255</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="geometry">
<rect>
<x>30</x>
<y>260</y>
<width>156</width>
<height>23</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
<widget class="QWidget" name="widget" native="true">
<property name="geometry">
<rect>
<x>230</x>
<y>10</y>
<width>101</width>
<height>271</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>76</height>
</size>
</property>
<property name="baseSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="autoFillBackground">
<bool>true</bool>
</property>
</widget>
<widget class="QLabel" name="label_7">
<property name="geometry">
<rect>
<x>10</x>
<y>220</y>
<width>16</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>C</string>
</property>
</widget>
<widget class="QSlider" name="cyanSlider">
<property name="geometry">
<rect>
<x>30</x>
<y>220</y>
<width>131</width>
<height>22</height>
</rect>
</property>
<property name="maximum">
<number>255</number>
</property>
<property name="singleStep">
<number>1</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
<widget class="QSpinBox" name="blackSpinbox">
<property name="geometry">
<rect>
<x>170</x>
<y>130</y>
<width>51</width>
<height>22</height>
</rect>
</property>
<property name="maximum">
<number>255</number>
</property>
</widget>
<widget class="QSpinBox" name="yellowSpinbox">
<property name="geometry">
<rect>
<x>170</x>
<y>160</y>
<width>51</width>
<height>22</height>
</rect>
</property>
<property name="maximum">
<number>255</number>
</property>
</widget>
<widget class="QSpinBox" name="magentaSpinbox">
<property name="geometry">
<rect>
<x>170</x>
<y>190</y>
<width>51</width>
<height>22</height>
</rect>
</property>
<property name="maximum">
<number>255</number>
</property>
</widget>
<widget class="QSpinBox" name="cyanSpinbox">
<property name="geometry">
<rect>
<x>170</x>
<y>220</y>
<width>51</width>
<height>22</height>
</rect>
</property>
<property name="maximum">
<number>255</number>
</property>
</widget>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>ColorPicker</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>185</x>
<y>291</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>ColorPicker</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>185</x>
<y>290</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>redSlider</sender>
<signal>valueChanged(int)</signal>
<receiver>redSpinbox</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>163</x>
<y>23</y>
</hint>
<hint type="destinationlabel">
<x>193</x>
<y>22</y>
</hint>
</hints>
</connection>
<connection>
<sender>redSpinbox</sender>
<signal>valueChanged(int)</signal>
<receiver>redSlider</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>185</x>
<y>11</y>
</hint>
<hint type="destinationlabel">
<x>163</x>
<y>16</y>
</hint>
</hints>
</connection>
<connection>
<sender>greenSlider</sender>
<signal>valueChanged(int)</signal>
<receiver>greenSpinbox</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>163</x>
<y>52</y>
</hint>
<hint type="destinationlabel">
<x>187</x>
<y>53</y>
</hint>
</hints>
</connection>
<connection>
<sender>greenSpinbox</sender>
<signal>valueChanged(int)</signal>
<receiver>greenSlider</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>182</x>
<y>46</y>
</hint>
<hint type="destinationlabel">
<x>163</x>
<y>46</y>
</hint>
</hints>
</connection>
<connection>
<sender>blueSlider</sender>
<signal>valueChanged(int)</signal>
<receiver>blueSpinbox</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>163</x>
<y>80</y>
</hint>
<hint type="destinationlabel">
<x>187</x>
<y>79</y>
</hint>
</hints>
</connection>
<connection>
<sender>blueSpinbox</sender>
<signal>valueChanged(int)</signal>
<receiver>blueSlider</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>179</x>
<y>81</y>
</hint>
<hint type="destinationlabel">
<x>163</x>
<y>78</y>
</hint>
</hints>
</connection>
<connection>
<sender>cyanSpinbox</sender>
<signal>valueChanged(int)</signal>
<receiver>cyanSlider</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>178</x>
<y>227</y>
</hint>
<hint type="destinationlabel">
<x>144</x>
<y>225</y>
</hint>
</hints>
</connection>
<connection>
<sender>cyanSlider</sender>
<signal>valueChanged(int)</signal>
<receiver>cyanSpinbox</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>143</x>
<y>236</y>
</hint>
<hint type="destinationlabel">
<x>193</x>
<y>241</y>
</hint>
</hints>
</connection>
<connection>
<sender>magentaSlider</sender>
<signal>valueChanged(int)</signal>
<receiver>magentaSpinbox</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>144</x>
<y>202</y>
</hint>
<hint type="destinationlabel">
<x>170</x>
<y>202</y>
</hint>
</hints>
</connection>
<connection>
<sender>magentaSpinbox</sender>
<signal>valueChanged(int)</signal>
<receiver>magentaSlider</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>191</x>
<y>191</y>
</hint>
<hint type="destinationlabel">
<x>143</x>
<y>195</y>
</hint>
</hints>
</connection>
<connection>
<sender>yellowSlider</sender>
<signal>valueChanged(int)</signal>
<receiver>yellowSpinbox</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>146</x>
<y>171</y>
</hint>
<hint type="destinationlabel">
<x>185</x>
<y>175</y>
</hint>
</hints>
</connection>
<connection>
<sender>yellowSpinbox</sender>
<signal>valueChanged(int)</signal>
<receiver>yellowSlider</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>181</x>
<y>168</y>
</hint>
<hint type="destinationlabel">
<x>122</x>
<y>170</y>
</hint>
</hints>
</connection>
<connection>
<sender>blackSlider</sender>
<signal>valueChanged(int)</signal>
<receiver>blackSpinbox</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>148</x>
<y>142</y>
</hint>
<hint type="destinationlabel">
<x>183</x>
<y>143</y>
</hint>
</hints>
</connection>
<connection>
<sender>blackSpinbox</sender>
<signal>valueChanged(int)</signal>
<receiver>blackSlider</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>185</x>
<y>136</y>
</hint>
<hint type="destinationlabel">
<x>143</x>
<y>136</y>
</hint>
</hints>
</connection>
</connections>
</ui>
I reached limit in chars used in this post.
Here is link to whole project: http://uploadfile.pl/pokaz/1252644---svem.html
I tried solutions with signals and slot, but it's not working for me - I guess that I do something wrong.
To obtain the values of some property with a QDialog after pressing the button that accepts the dialogue, it is not necessary to use signals. What you must do is execute the exec() function that generates a main loop so no line is executed after it, this function returns the accepted and rejected status that we can check with Accepted and Rejected, respectively.
But for this you must create the getter method that will be called color in ColorPicker:
*.h
public:
QColor color() const;
*.cpp
QColor ColorPicker::color() const
{
return mColor;
}
Then we rewrite the slot on_colorButton_clicked and the function setColor of ToolWindow:
void ToolWindow::on_colorButton_clicked()
{
if(colorPicker.exec() == ColorPicker::Accepted){
setColor(colorPicker.color());
}
}
void ToolWindow::setColor(QColor color)
{
ui->colorButton->setPalette(color);
}
The complete code can be found at the following link