我试图让QtCheckBox启用三态时在复选框中绘制一个X,当它的状态是偏的时候。我目前正在让Qt处理这种风格,因此它与操作系统相匹配,我希望保持这种方式并且只改变复选框的绘制方式。
我一直试图通过覆盖Qt使用的QStyle来做到这一点。我测试了Qt默认使用的样式:
>>> app = QtGui.QApplication()
>>> type(app.style())
PyQt4.QtGui.QCommonStyle
但是当我尝试运行app.setStyle(QtGui.QCommonStyle())
时,GUI会发生变化,最明显的是复选标记会从QCheckBox
es消失。
我查了QCommonStyle
,根据文档,它只绘制了所有样式共有的元素。
所以我有两个问题:
type(app.style())
显示所使用的样式实际上不是QCommonStyle
时,为什么QCommonStyle
会返回public class TodoAddActivity extends AppCompatActivity {
// Variables
private Context mContext;
private Realm realm;
private TodoModel task;
private Button button;
private EditText mTaskNameEditText;
private EditText mTaskDateEditText;
private EditText mTaskTimeEditText;
String time;
//private static List<TodoModel> tasks;
private RealmResults<TodoModel> results;
public final static String INTENT_KEY_ID = "taskId";
public final static String DATE_FORMAT = "dd/MMM/yy";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView( R.layout.todo_add_layout);
mContext = this;
realm=Realm.getDefaultInstance();
setUpUIViews();
showData();
final String taskId = getIntent().getStringExtra(INTENT_KEY_ID);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (taskId == null)
addTask();
else
editTask(taskId);
}
});
mTaskDateEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
datePicker(view);
}
});
mTaskDateEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if (b)
datePicker(view);
}
});
mTaskTimeEditText.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
timePicker( view );
}
} );
mTaskTimeEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if (b)
timePicker( view );
}
});
if (taskId != null) {
fillTask(realm.where(TodoModel.class).equalTo("id", taskId).findFirst());
button.setText("EDIT");
}
}
private void setUpUIViews() {
button = (Button) findViewById(R.id.activity_add_task_bt_add);
mTaskNameEditText = (EditText) findViewById(R.id.activity_add_task_et_name);
mTaskDateEditText = (EditText) findViewById(R.id.activity_add_task_et_date);
mTaskTimeEditText = (EditText) findViewById(R.id.activity_add_task_et_time);
}
public void showData(){
//tasks = new ArrayList<>(); //error
//RealmResults<TodoModel> tasksResult = realm.where(TodoModel.class).findAll();
//for (TodoModel task : tasksResult)
// tasks.add(task); //error*/
results = realm.where(TodoModel.class).findAll();
}
protected TodoModel getTask(int position) {
return results.get(position);
}
private void fillTask(TodoModel task) {
mTaskNameEditText.setText(task.getName());
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
mTaskDateEditText.setText(sdf.format(task.getDate()));
mTaskTimeEditText.setText(task.getTime());
}
private void addTask() {
String taskName = mTaskNameEditText.getText().toString();
String taskDate = mTaskDateEditText.getText().toString();
String taskTime = mTaskTimeEditText.getText().toString();
if (taskName.length() == 0 || taskName.equals("")) {
mTaskNameEditText.setError("Should be filled");
return;
}
if (taskDate.length() == 0 || taskDate.equals("")) {
mTaskDateEditText.setError("Should be filled");
return;
}
TodoModel task = getTask(taskName, taskDate,taskTime);
if (task == null) {
mTaskDateEditText.setError("Not a correct date");
return;
}
task.setDone(false);
addTask(task);
setResult(RESULT_OK);
finish();
}
protected void addTask(TodoModel task) {
realm.beginTransaction();
TodoModel newTask = realm.createObject(TodoModel.class, UUID.randomUUID().toString());
newTask.setName(task.getName());
newTask.setDate(task.getDate());
newTask.setTime(task.getTime());
//realm.copyToRealmOrUpdate(newTask);
realm.commitTransaction();
//realm.close();
}
private void editTask(String taskId) {
String taskName = mTaskNameEditText.getText().toString();
String taskDate = mTaskDateEditText.getText().toString();
String taskTime = mTaskTimeEditText.getText().toString();
if (taskName.length() == 0 || taskName.equals("")) {
mTaskNameEditText.setError("Should be filled");
return;
}
if (taskDate.length() == 0 || taskDate.equals("")) {
mTaskDateEditText.setError("Should be filled");
return;
}
if (taskTime.length() == 0 || taskTime.equals("")) {
mTaskTimeEditText.setError("Should be filled");
return;
}
TodoModel task = getTask(taskName, taskDate,taskTime);
if (task == null) {
mTaskDateEditText.setError("Not a correct date");
return;
}
task.setId(taskId);
updateTask(task);
setResult(RESULT_OK);
finish();
}
protected void updateTask(TodoModel task) {
realm.beginTransaction();
TodoModel newTask = realm.where(TodoModel.class).equalTo("id", task.getId()).findFirst();
newTask.setDate(task.getDate());
newTask.setDone(task.isDone());
newTask.setName(task.getName());
realm.commitTransaction();
}
private void datePicker(final View view) {
Calendar currentTime = Calendar.getInstance();
int year = currentTime.get(Calendar.YEAR);
int monthOfYear = currentTime.get(Calendar.MONTH);
int dayOfMonth = currentTime.get(Calendar.DAY_OF_MONTH);
DatePickerDialog mDatePickerDialog = new DatePickerDialog(mContext, 0, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, monthOfYear, dayOfMonth);
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
((EditText) view).setText(sdf.format(calendar.getTime()));
}
}, year, monthOfYear, dayOfMonth);
mDatePickerDialog.setTitle("Select Time");
mDatePickerDialog.show();
}
private void timePicker(final View view) {
Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(mContext,0, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
mTaskTimeEditText.setText( selectedHour + ":" + selectedMinute);
}
}, hour, minute, true);//Yes 24 hour time
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
private TodoModel getTask(String name, String dateString,String time) {
TodoModel task = new TodoModel();
try {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
task.setName(name);
task.setDate(sdf.parse(dateString));
task.setTime(time );
task.setDone(false);
return task;
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home)
onBackPressed();
return super.onOptionsItemSelected(item);
}
}
?我正在使用PyQt4。
答案 0 :(得分:2)
为什么type(app.style())返回QCommonStyle,当它出现时 使用的样式实际上不是QCommonStyle?
像样式一样使用的类通常继承QCommonStyle,例如在我的例子中我有以下样式:
for key in QtGui.QStyleFactory.keys():
st = QtGui.QStyleFactory.create(key)
print(key, st.metaObject().className(), type(app.style()))
输出:
Adwaita-Dark Adwaita::Style <class 'PyQt4.QtGui.QCommonStyle'>
Adwaita Adwaita::Style <class 'PyQt4.QtGui.QCommonStyle'>
Breeze Breeze::Style <class 'PyQt4.QtGui.QCommonStyle'>
bb10dark QBB10DarkStyle <class 'PyQt4.QtGui.QCommonStyle'>
bb10bright QBB10BrightStyle <class 'PyQt4.QtGui.QCommonStyle'>
cleanlooks QCleanlooksStyle <class 'PyQt4.QtGui.QCommonStyle'>
gtk2 QGtkStyle <class 'PyQt4.QtGui.QCommonStyle'>
cde QCDEStyle <class 'PyQt4.QtGui.QCommonStyle'>
motif QMotifStyle <class 'PyQt4.QtGui.QCommonStyle'>
plastique QPlastiqueStyle <class 'PyQt4.QtGui.QCommonStyle'>
qt5ct-style Qt5CTProxyStyle <class 'PyQt4.QtGui.QCommonStyle'>
QtCurve QtCurve::Style <class 'PyQt4.QtGui.QCommonStyle'>
Windows QWindowsStyle <class 'PyQt4.QtGui.QCommonStyle'>
Fusion QFusionStyle <class 'PyQt4.QtGui.QCommonStyle'>
正如我们观察到的那样都属于那种类型。
我如何实际获得Qt使用的样式?
上一部分已经回答:
print(QtGui.QApplication.style().metaObject().className())
就我而言:
Adwaita::Style